2011-05-31 Gabriel Charette <gchare@google.com>
[official-gcc.git] / gcc / tree.c
blob841c837d69436c1b4b5780359a007e3d3b5fefed
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "tm_p.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "toplev.h"
42 #include "ggc.h"
43 #include "hashtab.h"
44 #include "filenames.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "tree-inline.h"
49 #include "tree-iterator.h"
50 #include "basic-block.h"
51 #include "tree-flow.h"
52 #include "params.h"
53 #include "pointer-set.h"
54 #include "tree-pass.h"
55 #include "langhooks-def.h"
56 #include "diagnostic.h"
57 #include "tree-diagnostic.h"
58 #include "tree-pretty-print.h"
59 #include "cgraph.h"
60 #include "timevar.h"
61 #include "except.h"
62 #include "debug.h"
63 #include "intl.h"
65 /* Tree code classes. */
67 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
68 #define END_OF_BASE_TREE_CODES tcc_exceptional,
70 const enum tree_code_class tree_code_type[] = {
71 #include "all-tree.def"
74 #undef DEFTREECODE
75 #undef END_OF_BASE_TREE_CODES
77 /* Table indexed by tree code giving number of expression
78 operands beyond the fixed part of the node structure.
79 Not used for types or decls. */
81 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
82 #define END_OF_BASE_TREE_CODES 0,
84 const unsigned char tree_code_length[] = {
85 #include "all-tree.def"
88 #undef DEFTREECODE
89 #undef END_OF_BASE_TREE_CODES
91 /* Names of tree components.
92 Used for printing out the tree and error messages. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
94 #define END_OF_BASE_TREE_CODES "@dummy",
96 const char *const tree_code_name[] = {
97 #include "all-tree.def"
100 #undef DEFTREECODE
101 #undef END_OF_BASE_TREE_CODES
103 /* Each tree code class has an associated string representation.
104 These must correspond to the tree_code_class entries. */
106 const char *const tree_code_class_strings[] =
108 "exceptional",
109 "constant",
110 "type",
111 "declaration",
112 "reference",
113 "comparison",
114 "unary",
115 "binary",
116 "statement",
117 "vl_exp",
118 "expression"
121 /* obstack.[ch] explicitly declined to prototype this. */
122 extern int _obstack_allocated_p (struct obstack *h, void *obj);
124 #ifdef GATHER_STATISTICS
125 /* Statistics-gathering stuff. */
127 static int tree_code_counts[MAX_TREE_CODES];
128 int tree_node_counts[(int) all_kinds];
129 int tree_node_sizes[(int) all_kinds];
131 /* Keep in sync with tree.h:enum tree_node_kind. */
132 static const char * const tree_node_kind_names[] = {
133 "decls",
134 "types",
135 "blocks",
136 "stmts",
137 "refs",
138 "exprs",
139 "constants",
140 "identifiers",
141 "vecs",
142 "binfos",
143 "ssa names",
144 "constructors",
145 "random kinds",
146 "lang_decl kinds",
147 "lang_type kinds",
148 "omp clauses",
150 #endif /* GATHER_STATISTICS */
152 /* Unique id for next decl created. */
153 static GTY(()) int next_decl_uid;
154 /* Unique id for next type created. */
155 static GTY(()) int next_type_uid = 1;
156 /* Unique id for next debug decl created. Use negative numbers,
157 to catch erroneous uses. */
158 static GTY(()) int next_debug_decl_uid;
160 /* Since we cannot rehash a type after it is in the table, we have to
161 keep the hash code. */
163 struct GTY(()) type_hash {
164 unsigned long hash;
165 tree type;
168 /* Initial size of the hash table (rounded to next prime). */
169 #define TYPE_HASH_INITIAL_SIZE 1000
171 /* Now here is the hash table. When recording a type, it is added to
172 the slot whose index is the hash code. Note that the hash table is
173 used for several kinds of types (function types, array types and
174 array index range types, for now). While all these live in the
175 same table, they are completely independent, and the hash code is
176 computed differently for each of these. */
178 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
179 htab_t type_hash_table;
181 /* Hash table and temporary node for larger integer const values. */
182 static GTY (()) tree int_cst_node;
183 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
184 htab_t int_cst_hash_table;
186 /* Hash table for optimization flags and target option flags. Use the same
187 hash table for both sets of options. Nodes for building the current
188 optimization and target option nodes. The assumption is most of the time
189 the options created will already be in the hash table, so we avoid
190 allocating and freeing up a node repeatably. */
191 static GTY (()) tree cl_optimization_node;
192 static GTY (()) tree cl_target_option_node;
193 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
194 htab_t cl_option_hash_table;
196 /* General tree->tree mapping structure for use in hash tables. */
199 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
200 htab_t debug_expr_for_decl;
202 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
203 htab_t value_expr_for_decl;
205 static GTY ((if_marked ("tree_priority_map_marked_p"),
206 param_is (struct tree_priority_map)))
207 htab_t init_priority_for_decl;
209 static void set_type_quals (tree, int);
210 static int type_hash_eq (const void *, const void *);
211 static hashval_t type_hash_hash (const void *);
212 static hashval_t int_cst_hash_hash (const void *);
213 static int int_cst_hash_eq (const void *, const void *);
214 static hashval_t cl_option_hash_hash (const void *);
215 static int cl_option_hash_eq (const void *, const void *);
216 static void print_type_hash_statistics (void);
217 static void print_debug_expr_statistics (void);
218 static void print_value_expr_statistics (void);
219 static int type_hash_marked_p (const void *);
220 static unsigned int type_hash_list (const_tree, hashval_t);
221 static unsigned int attribute_hash_list (const_tree, hashval_t);
223 tree global_trees[TI_MAX];
224 tree integer_types[itk_none];
226 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
228 /* Number of operands for each OpenMP clause. */
229 unsigned const char omp_clause_num_ops[] =
231 0, /* OMP_CLAUSE_ERROR */
232 1, /* OMP_CLAUSE_PRIVATE */
233 1, /* OMP_CLAUSE_SHARED */
234 1, /* OMP_CLAUSE_FIRSTPRIVATE */
235 2, /* OMP_CLAUSE_LASTPRIVATE */
236 4, /* OMP_CLAUSE_REDUCTION */
237 1, /* OMP_CLAUSE_COPYIN */
238 1, /* OMP_CLAUSE_COPYPRIVATE */
239 1, /* OMP_CLAUSE_IF */
240 1, /* OMP_CLAUSE_NUM_THREADS */
241 1, /* OMP_CLAUSE_SCHEDULE */
242 0, /* OMP_CLAUSE_NOWAIT */
243 0, /* OMP_CLAUSE_ORDERED */
244 0, /* OMP_CLAUSE_DEFAULT */
245 3, /* OMP_CLAUSE_COLLAPSE */
246 0 /* OMP_CLAUSE_UNTIED */
249 const char * const omp_clause_code_name[] =
251 "error_clause",
252 "private",
253 "shared",
254 "firstprivate",
255 "lastprivate",
256 "reduction",
257 "copyin",
258 "copyprivate",
259 "if",
260 "num_threads",
261 "schedule",
262 "nowait",
263 "ordered",
264 "default",
265 "collapse",
266 "untied"
270 /* Return the tree node structure used by tree code CODE. */
272 enum tree_node_structure_enum
273 tree_node_structure_for_code (enum tree_code code)
275 switch (TREE_CODE_CLASS (code))
277 case tcc_declaration:
279 switch (code)
281 case FIELD_DECL:
282 return TS_FIELD_DECL;
283 case PARM_DECL:
284 return TS_PARM_DECL;
285 case VAR_DECL:
286 return TS_VAR_DECL;
287 case LABEL_DECL:
288 return TS_LABEL_DECL;
289 case RESULT_DECL:
290 return TS_RESULT_DECL;
291 case DEBUG_EXPR_DECL:
292 return TS_DECL_WRTL;
293 case CONST_DECL:
294 return TS_CONST_DECL;
295 case TYPE_DECL:
296 return TS_TYPE_DECL;
297 case FUNCTION_DECL:
298 return TS_FUNCTION_DECL;
299 case TRANSLATION_UNIT_DECL:
300 return TS_TRANSLATION_UNIT_DECL;
301 default:
302 return TS_DECL_NON_COMMON;
305 case tcc_type:
306 return TS_TYPE_NON_COMMON;
307 case tcc_reference:
308 case tcc_comparison:
309 case tcc_unary:
310 case tcc_binary:
311 case tcc_expression:
312 case tcc_statement:
313 case tcc_vl_exp:
314 return TS_EXP;
315 default: /* tcc_constant and tcc_exceptional */
316 break;
318 switch (code)
320 /* tcc_constant cases. */
321 case INTEGER_CST: return TS_INT_CST;
322 case REAL_CST: return TS_REAL_CST;
323 case FIXED_CST: return TS_FIXED_CST;
324 case COMPLEX_CST: return TS_COMPLEX;
325 case VECTOR_CST: return TS_VECTOR;
326 case STRING_CST: return TS_STRING;
327 /* tcc_exceptional cases. */
328 case ERROR_MARK: return TS_COMMON;
329 case IDENTIFIER_NODE: return TS_IDENTIFIER;
330 case TREE_LIST: return TS_LIST;
331 case TREE_VEC: return TS_VEC;
332 case SSA_NAME: return TS_SSA_NAME;
333 case PLACEHOLDER_EXPR: return TS_COMMON;
334 case STATEMENT_LIST: return TS_STATEMENT_LIST;
335 case BLOCK: return TS_BLOCK;
336 case CONSTRUCTOR: return TS_CONSTRUCTOR;
337 case TREE_BINFO: return TS_BINFO;
338 case OMP_CLAUSE: return TS_OMP_CLAUSE;
339 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
340 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
342 default:
343 return LAST_TS_ENUM;
348 /* Mark tree code CODE as having the structure code TS_CODE and all its base
349 structures in the array `tree_contains_struct'. */
351 void
352 mark_ts_structures_for (enum tree_code code,
353 enum tree_node_structure_enum ts_code)
355 /* Mark the TS structure itself. */
356 tree_contains_struct[code][ts_code] = 1;
358 /* Mark all the structures that TS is derived from. */
359 switch (ts_code)
361 case TS_TYPED:
362 case TS_BLOCK:
363 MARK_TS_BASE (code);
364 break;
366 case TS_COMMON:
367 case TS_INT_CST:
368 case TS_REAL_CST:
369 case TS_FIXED_CST:
370 case TS_VECTOR:
371 case TS_STRING:
372 case TS_COMPLEX:
373 case TS_SSA_NAME:
374 case TS_CONSTRUCTOR:
375 case TS_EXP:
376 case TS_IDENTIFIER:
377 case TS_STATEMENT_LIST:
378 MARK_TS_TYPED (code);
379 break;
381 case TS_DECL_MINIMAL:
382 case TS_TYPE_COMMON:
383 case TS_LIST:
384 case TS_VEC:
385 case TS_BINFO:
386 case TS_OMP_CLAUSE:
387 case TS_OPTIMIZATION:
388 case TS_TARGET_OPTION:
389 MARK_TS_COMMON (code);
390 break;
392 case TS_TYPE_WITH_LANG_SPECIFIC:
393 MARK_TS_TYPE_COMMON (code);
394 break;
396 case TS_TYPE_NON_COMMON:
397 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
398 break;
400 case TS_DECL_COMMON:
401 MARK_TS_DECL_MINIMAL (code);
402 break;
404 case TS_DECL_WRTL:
405 case TS_CONST_DECL:
406 MARK_TS_DECL_COMMON (code);
407 break;
409 case TS_DECL_NON_COMMON:
410 MARK_TS_DECL_WITH_VIS (code);
411 break;
413 case TS_DECL_WITH_VIS:
414 case TS_PARM_DECL:
415 case TS_LABEL_DECL:
416 case TS_RESULT_DECL:
417 MARK_TS_DECL_WRTL (code);
418 break;
420 case TS_FIELD_DECL:
421 MARK_TS_DECL_COMMON (code);
422 break;
424 case TS_VAR_DECL:
425 MARK_TS_DECL_WITH_VIS (code);
426 break;
428 case TS_TYPE_DECL:
429 case TS_FUNCTION_DECL:
430 MARK_TS_DECL_NON_COMMON (code);
431 break;
433 case TS_TRANSLATION_UNIT_DECL:
434 MARK_TS_DECL_COMMON (code);
435 break;
437 default:
438 gcc_unreachable ();
443 /* Initialize tree_contains_struct to describe the hierarchy of tree
444 nodes. */
446 static void
447 initialize_tree_contains_struct (void)
449 unsigned i;
451 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
453 enum tree_code code;
454 enum tree_node_structure_enum ts_code;
456 code = (enum tree_code) i;
457 ts_code = tree_node_structure_for_code (code);
458 gcc_assert (ts_code != LAST_TS_ENUM);
460 /* Mark the TS structure and its bases. */
461 mark_ts_structures_for (code, ts_code);
464 /* Basic consistency checks for attributes used in fold. */
465 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
466 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
467 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
468 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
469 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
470 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
471 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
472 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
473 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
474 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
475 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
476 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
477 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
478 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
479 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
480 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
481 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
482 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
483 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
484 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
485 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
486 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
487 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
488 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
489 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
490 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
491 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
492 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
493 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
494 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
495 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
496 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
497 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
498 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
499 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
500 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
501 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
502 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
506 /* Init tree.c. */
508 void
509 init_ttree (void)
511 /* Initialize the hash table of types. */
512 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
513 type_hash_eq, 0);
515 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
516 tree_decl_map_eq, 0);
518 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
519 tree_decl_map_eq, 0);
520 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
521 tree_priority_map_eq, 0);
523 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
524 int_cst_hash_eq, NULL);
526 int_cst_node = make_node (INTEGER_CST);
528 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
529 cl_option_hash_eq, NULL);
531 cl_optimization_node = make_node (OPTIMIZATION_NODE);
532 cl_target_option_node = make_node (TARGET_OPTION_NODE);
534 /* Initialize the tree_contains_struct array. */
535 initialize_tree_contains_struct ();
536 lang_hooks.init_ts ();
540 /* The name of the object as the assembler will see it (but before any
541 translations made by ASM_OUTPUT_LABELREF). Often this is the same
542 as DECL_NAME. It is an IDENTIFIER_NODE. */
543 tree
544 decl_assembler_name (tree decl)
546 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
547 lang_hooks.set_decl_assembler_name (decl);
548 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
551 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
553 bool
554 decl_assembler_name_equal (tree decl, const_tree asmname)
556 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
557 const char *decl_str;
558 const char *asmname_str;
559 bool test = false;
561 if (decl_asmname == asmname)
562 return true;
564 decl_str = IDENTIFIER_POINTER (decl_asmname);
565 asmname_str = IDENTIFIER_POINTER (asmname);
568 /* If the target assembler name was set by the user, things are trickier.
569 We have a leading '*' to begin with. After that, it's arguable what
570 is the correct thing to do with -fleading-underscore. Arguably, we've
571 historically been doing the wrong thing in assemble_alias by always
572 printing the leading underscore. Since we're not changing that, make
573 sure user_label_prefix follows the '*' before matching. */
574 if (decl_str[0] == '*')
576 size_t ulp_len = strlen (user_label_prefix);
578 decl_str ++;
580 if (ulp_len == 0)
581 test = true;
582 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
583 decl_str += ulp_len, test=true;
584 else
585 decl_str --;
587 if (asmname_str[0] == '*')
589 size_t ulp_len = strlen (user_label_prefix);
591 asmname_str ++;
593 if (ulp_len == 0)
594 test = true;
595 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
596 asmname_str += ulp_len, test=true;
597 else
598 asmname_str --;
601 if (!test)
602 return false;
603 return strcmp (decl_str, asmname_str) == 0;
606 /* Hash asmnames ignoring the user specified marks. */
608 hashval_t
609 decl_assembler_name_hash (const_tree asmname)
611 if (IDENTIFIER_POINTER (asmname)[0] == '*')
613 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
614 size_t ulp_len = strlen (user_label_prefix);
616 if (ulp_len == 0)
618 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
619 decl_str += ulp_len;
621 return htab_hash_string (decl_str);
624 return htab_hash_string (IDENTIFIER_POINTER (asmname));
627 /* Compute the number of bytes occupied by a tree with code CODE.
628 This function cannot be used for nodes that have variable sizes,
629 including TREE_VEC, STRING_CST, and CALL_EXPR. */
630 size_t
631 tree_code_size (enum tree_code code)
633 switch (TREE_CODE_CLASS (code))
635 case tcc_declaration: /* A decl node */
637 switch (code)
639 case FIELD_DECL:
640 return sizeof (struct tree_field_decl);
641 case PARM_DECL:
642 return sizeof (struct tree_parm_decl);
643 case VAR_DECL:
644 return sizeof (struct tree_var_decl);
645 case LABEL_DECL:
646 return sizeof (struct tree_label_decl);
647 case RESULT_DECL:
648 return sizeof (struct tree_result_decl);
649 case CONST_DECL:
650 return sizeof (struct tree_const_decl);
651 case TYPE_DECL:
652 return sizeof (struct tree_type_decl);
653 case FUNCTION_DECL:
654 return sizeof (struct tree_function_decl);
655 case DEBUG_EXPR_DECL:
656 return sizeof (struct tree_decl_with_rtl);
657 default:
658 return sizeof (struct tree_decl_non_common);
662 case tcc_type: /* a type node */
663 return sizeof (struct tree_type_non_common);
665 case tcc_reference: /* a reference */
666 case tcc_expression: /* an expression */
667 case tcc_statement: /* an expression with side effects */
668 case tcc_comparison: /* a comparison expression */
669 case tcc_unary: /* a unary arithmetic expression */
670 case tcc_binary: /* a binary arithmetic expression */
671 return (sizeof (struct tree_exp)
672 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
674 case tcc_constant: /* a constant */
675 switch (code)
677 case INTEGER_CST: return sizeof (struct tree_int_cst);
678 case REAL_CST: return sizeof (struct tree_real_cst);
679 case FIXED_CST: return sizeof (struct tree_fixed_cst);
680 case COMPLEX_CST: return sizeof (struct tree_complex);
681 case VECTOR_CST: return sizeof (struct tree_vector);
682 case STRING_CST: gcc_unreachable ();
683 default:
684 return lang_hooks.tree_size (code);
687 case tcc_exceptional: /* something random, like an identifier. */
688 switch (code)
690 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
691 case TREE_LIST: return sizeof (struct tree_list);
693 case ERROR_MARK:
694 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
696 case TREE_VEC:
697 case OMP_CLAUSE: gcc_unreachable ();
699 case SSA_NAME: return sizeof (struct tree_ssa_name);
701 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
702 case BLOCK: return sizeof (struct tree_block);
703 case CONSTRUCTOR: return sizeof (struct tree_constructor);
704 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
705 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
707 default:
708 return lang_hooks.tree_size (code);
711 default:
712 gcc_unreachable ();
716 /* Compute the number of bytes occupied by NODE. This routine only
717 looks at TREE_CODE, except for those nodes that have variable sizes. */
718 size_t
719 tree_size (const_tree node)
721 const enum tree_code code = TREE_CODE (node);
722 switch (code)
724 case TREE_BINFO:
725 return (offsetof (struct tree_binfo, base_binfos)
726 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
728 case TREE_VEC:
729 return (sizeof (struct tree_vec)
730 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
732 case STRING_CST:
733 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
735 case OMP_CLAUSE:
736 return (sizeof (struct tree_omp_clause)
737 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
738 * sizeof (tree));
740 default:
741 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
742 return (sizeof (struct tree_exp)
743 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
744 else
745 return tree_code_size (code);
749 /* Record interesting allocation statistics for a tree node with CODE
750 and LENGTH. */
752 static void
753 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
754 size_t length ATTRIBUTE_UNUSED)
756 #ifdef GATHER_STATISTICS
757 enum tree_code_class type = TREE_CODE_CLASS (code);
758 tree_node_kind kind;
760 switch (type)
762 case tcc_declaration: /* A decl node */
763 kind = d_kind;
764 break;
766 case tcc_type: /* a type node */
767 kind = t_kind;
768 break;
770 case tcc_statement: /* an expression with side effects */
771 kind = s_kind;
772 break;
774 case tcc_reference: /* a reference */
775 kind = r_kind;
776 break;
778 case tcc_expression: /* an expression */
779 case tcc_comparison: /* a comparison expression */
780 case tcc_unary: /* a unary arithmetic expression */
781 case tcc_binary: /* a binary arithmetic expression */
782 kind = e_kind;
783 break;
785 case tcc_constant: /* a constant */
786 kind = c_kind;
787 break;
789 case tcc_exceptional: /* something random, like an identifier. */
790 switch (code)
792 case IDENTIFIER_NODE:
793 kind = id_kind;
794 break;
796 case TREE_VEC:
797 kind = vec_kind;
798 break;
800 case TREE_BINFO:
801 kind = binfo_kind;
802 break;
804 case SSA_NAME:
805 kind = ssa_name_kind;
806 break;
808 case BLOCK:
809 kind = b_kind;
810 break;
812 case CONSTRUCTOR:
813 kind = constr_kind;
814 break;
816 case OMP_CLAUSE:
817 kind = omp_clause_kind;
818 break;
820 default:
821 kind = x_kind;
822 break;
824 break;
826 case tcc_vl_exp:
827 kind = e_kind;
828 break;
830 default:
831 gcc_unreachable ();
834 tree_code_counts[(int) code]++;
835 tree_node_counts[(int) kind]++;
836 tree_node_sizes[(int) kind] += length;
837 #endif
840 /* Allocate and return a new UID from the DECL_UID namespace. */
843 allocate_decl_uid (void)
845 return next_decl_uid++;
848 /* Return a newly allocated node of code CODE. For decl and type
849 nodes, some other fields are initialized. The rest of the node is
850 initialized to zero. This function cannot be used for TREE_VEC or
851 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
853 Achoo! I got a code in the node. */
855 tree
856 make_node_stat (enum tree_code code MEM_STAT_DECL)
858 tree t;
859 enum tree_code_class type = TREE_CODE_CLASS (code);
860 size_t length = tree_code_size (code);
862 record_node_allocation_statistics (code, length);
864 t = ggc_alloc_zone_cleared_tree_node_stat (
865 (code == IDENTIFIER_NODE) ? &tree_id_zone : &tree_zone,
866 length PASS_MEM_STAT);
867 TREE_SET_CODE (t, code);
869 switch (type)
871 case tcc_statement:
872 TREE_SIDE_EFFECTS (t) = 1;
873 break;
875 case tcc_declaration:
876 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
878 if (code == FUNCTION_DECL)
880 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
881 DECL_MODE (t) = FUNCTION_MODE;
883 else
884 DECL_ALIGN (t) = 1;
886 DECL_SOURCE_LOCATION (t) = input_location;
887 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
888 DECL_UID (t) = --next_debug_decl_uid;
889 else
891 DECL_UID (t) = allocate_decl_uid ();
892 SET_DECL_PT_UID (t, -1);
894 if (TREE_CODE (t) == LABEL_DECL)
895 LABEL_DECL_UID (t) = -1;
897 if (lang_hooks.pph_catch_tree)
898 lang_hooks.pph_catch_tree (t);
899 break;
901 case tcc_type:
902 TYPE_UID (t) = next_type_uid++;
903 TYPE_ALIGN (t) = BITS_PER_UNIT;
904 TYPE_USER_ALIGN (t) = 0;
905 TYPE_MAIN_VARIANT (t) = t;
906 TYPE_CANONICAL (t) = t;
908 /* Default to no attributes for type, but let target change that. */
909 TYPE_ATTRIBUTES (t) = NULL_TREE;
910 targetm.set_default_type_attributes (t);
912 /* We have not yet computed the alias set for this type. */
913 TYPE_ALIAS_SET (t) = -1;
914 break;
916 case tcc_constant:
917 TREE_CONSTANT (t) = 1;
918 break;
920 case tcc_expression:
921 switch (code)
923 case INIT_EXPR:
924 case MODIFY_EXPR:
925 case VA_ARG_EXPR:
926 case PREDECREMENT_EXPR:
927 case PREINCREMENT_EXPR:
928 case POSTDECREMENT_EXPR:
929 case POSTINCREMENT_EXPR:
930 /* All of these have side-effects, no matter what their
931 operands are. */
932 TREE_SIDE_EFFECTS (t) = 1;
933 break;
935 default:
936 break;
938 break;
940 default:
941 /* Other classes need no special treatment. */
942 break;
945 return t;
948 /* Return a new node with the same contents as NODE except that its
949 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
951 tree
952 copy_node_stat (tree node MEM_STAT_DECL)
954 tree t;
955 enum tree_code code = TREE_CODE (node);
956 size_t length;
958 gcc_assert (code != STATEMENT_LIST);
960 length = tree_size (node);
961 record_node_allocation_statistics (code, length);
962 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
963 memcpy (t, node, length);
965 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
966 TREE_CHAIN (t) = 0;
967 TREE_ASM_WRITTEN (t) = 0;
968 TREE_VISITED (t) = 0;
969 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
970 *DECL_VAR_ANN_PTR (t) = 0;
972 if (TREE_CODE_CLASS (code) == tcc_declaration)
974 if (code == DEBUG_EXPR_DECL)
975 DECL_UID (t) = --next_debug_decl_uid;
976 else
978 DECL_UID (t) = allocate_decl_uid ();
979 if (DECL_PT_UID_SET_P (node))
980 SET_DECL_PT_UID (t, DECL_PT_UID (node));
982 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
983 && DECL_HAS_VALUE_EXPR_P (node))
985 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
986 DECL_HAS_VALUE_EXPR_P (t) = 1;
988 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
990 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
991 DECL_HAS_INIT_PRIORITY_P (t) = 1;
994 else if (TREE_CODE_CLASS (code) == tcc_type)
996 TYPE_UID (t) = next_type_uid++;
997 /* The following is so that the debug code for
998 the copy is different from the original type.
999 The two statements usually duplicate each other
1000 (because they clear fields of the same union),
1001 but the optimizer should catch that. */
1002 TYPE_SYMTAB_POINTER (t) = 0;
1003 TYPE_SYMTAB_ADDRESS (t) = 0;
1005 /* Do not copy the values cache. */
1006 if (TYPE_CACHED_VALUES_P(t))
1008 TYPE_CACHED_VALUES_P (t) = 0;
1009 TYPE_CACHED_VALUES (t) = NULL_TREE;
1013 return t;
1016 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1017 For example, this can copy a list made of TREE_LIST nodes. */
1019 tree
1020 copy_list (tree list)
1022 tree head;
1023 tree prev, next;
1025 if (list == 0)
1026 return 0;
1028 head = prev = copy_node (list);
1029 next = TREE_CHAIN (list);
1030 while (next)
1032 TREE_CHAIN (prev) = copy_node (next);
1033 prev = TREE_CHAIN (prev);
1034 next = TREE_CHAIN (next);
1036 return head;
1040 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1042 tree
1043 build_int_cst (tree type, HOST_WIDE_INT low)
1045 /* Support legacy code. */
1046 if (!type)
1047 type = integer_type_node;
1049 return double_int_to_tree (type, shwi_to_double_int (low));
1052 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1054 tree
1055 build_int_cst_type (tree type, HOST_WIDE_INT low)
1057 gcc_assert (type);
1059 return double_int_to_tree (type, shwi_to_double_int (low));
1062 /* Constructs tree in type TYPE from with value given by CST. Signedness
1063 of CST is assumed to be the same as the signedness of TYPE. */
1065 tree
1066 double_int_to_tree (tree type, double_int cst)
1068 /* Size types *are* sign extended. */
1069 bool sign_extended_type = (!TYPE_UNSIGNED (type)
1070 || (TREE_CODE (type) == INTEGER_TYPE
1071 && TYPE_IS_SIZETYPE (type)));
1073 cst = double_int_ext (cst, TYPE_PRECISION (type), !sign_extended_type);
1075 return build_int_cst_wide (type, cst.low, cst.high);
1078 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1079 to be the same as the signedness of TYPE. */
1081 bool
1082 double_int_fits_to_tree_p (const_tree type, double_int cst)
1084 /* Size types *are* sign extended. */
1085 bool sign_extended_type = (!TYPE_UNSIGNED (type)
1086 || (TREE_CODE (type) == INTEGER_TYPE
1087 && TYPE_IS_SIZETYPE (type)));
1089 double_int ext
1090 = double_int_ext (cst, TYPE_PRECISION (type), !sign_extended_type);
1092 return double_int_equal_p (cst, ext);
1095 /* We force the double_int CST to the range of the type TYPE by sign or
1096 zero extending it. OVERFLOWABLE indicates if we are interested in
1097 overflow of the value, when >0 we are only interested in signed
1098 overflow, for <0 we are interested in any overflow. OVERFLOWED
1099 indicates whether overflow has already occurred. CONST_OVERFLOWED
1100 indicates whether constant overflow has already occurred. We force
1101 T's value to be within range of T's type (by setting to 0 or 1 all
1102 the bits outside the type's range). We set TREE_OVERFLOWED if,
1103 OVERFLOWED is nonzero,
1104 or OVERFLOWABLE is >0 and signed overflow occurs
1105 or OVERFLOWABLE is <0 and any overflow occurs
1106 We return a new tree node for the extended double_int. The node
1107 is shared if no overflow flags are set. */
1110 tree
1111 force_fit_type_double (tree type, double_int cst, int overflowable,
1112 bool overflowed)
1114 bool sign_extended_type;
1116 /* Size types *are* sign extended. */
1117 sign_extended_type = (!TYPE_UNSIGNED (type)
1118 || (TREE_CODE (type) == INTEGER_TYPE
1119 && TYPE_IS_SIZETYPE (type)));
1121 /* If we need to set overflow flags, return a new unshared node. */
1122 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1124 if (overflowed
1125 || overflowable < 0
1126 || (overflowable > 0 && sign_extended_type))
1128 tree t = make_node (INTEGER_CST);
1129 TREE_INT_CST (t) = double_int_ext (cst, TYPE_PRECISION (type),
1130 !sign_extended_type);
1131 TREE_TYPE (t) = type;
1132 TREE_OVERFLOW (t) = 1;
1133 return t;
1137 /* Else build a shared node. */
1138 return double_int_to_tree (type, cst);
1141 /* These are the hash table functions for the hash table of INTEGER_CST
1142 nodes of a sizetype. */
1144 /* Return the hash code code X, an INTEGER_CST. */
1146 static hashval_t
1147 int_cst_hash_hash (const void *x)
1149 const_tree const t = (const_tree) x;
1151 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1152 ^ htab_hash_pointer (TREE_TYPE (t)));
1155 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1156 is the same as that given by *Y, which is the same. */
1158 static int
1159 int_cst_hash_eq (const void *x, const void *y)
1161 const_tree const xt = (const_tree) x;
1162 const_tree const yt = (const_tree) y;
1164 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1165 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1166 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1169 /* Create an INT_CST node of TYPE and value HI:LOW.
1170 The returned node is always shared. For small integers we use a
1171 per-type vector cache, for larger ones we use a single hash table. */
1173 tree
1174 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1176 tree t;
1177 int ix = -1;
1178 int limit = 0;
1180 gcc_assert (type);
1182 switch (TREE_CODE (type))
1184 case NULLPTR_TYPE:
1185 gcc_assert (hi == 0 && low == 0);
1186 /* Fallthru. */
1188 case POINTER_TYPE:
1189 case REFERENCE_TYPE:
1190 /* Cache NULL pointer. */
1191 if (!hi && !low)
1193 limit = 1;
1194 ix = 0;
1196 break;
1198 case BOOLEAN_TYPE:
1199 /* Cache false or true. */
1200 limit = 2;
1201 if (!hi && low < 2)
1202 ix = low;
1203 break;
1205 case INTEGER_TYPE:
1206 case OFFSET_TYPE:
1207 if (TYPE_UNSIGNED (type))
1209 /* Cache 0..N */
1210 limit = INTEGER_SHARE_LIMIT;
1211 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1212 ix = low;
1214 else
1216 /* Cache -1..N */
1217 limit = INTEGER_SHARE_LIMIT + 1;
1218 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1219 ix = low + 1;
1220 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1221 ix = 0;
1223 break;
1225 case ENUMERAL_TYPE:
1226 break;
1228 default:
1229 gcc_unreachable ();
1232 if (ix >= 0)
1234 /* Look for it in the type's vector of small shared ints. */
1235 if (!TYPE_CACHED_VALUES_P (type))
1237 TYPE_CACHED_VALUES_P (type) = 1;
1238 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1241 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1242 if (t)
1244 /* Make sure no one is clobbering the shared constant. */
1245 gcc_assert (TREE_TYPE (t) == type);
1246 gcc_assert (TREE_INT_CST_LOW (t) == low);
1247 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1249 else
1251 /* Create a new shared int. */
1252 t = make_node (INTEGER_CST);
1254 TREE_INT_CST_LOW (t) = low;
1255 TREE_INT_CST_HIGH (t) = hi;
1256 TREE_TYPE (t) = type;
1258 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1261 else
1263 /* Use the cache of larger shared ints. */
1264 void **slot;
1266 TREE_INT_CST_LOW (int_cst_node) = low;
1267 TREE_INT_CST_HIGH (int_cst_node) = hi;
1268 TREE_TYPE (int_cst_node) = type;
1270 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1271 t = (tree) *slot;
1272 if (!t)
1274 /* Insert this one into the hash table. */
1275 t = int_cst_node;
1276 *slot = t;
1277 /* Make a new node for next time round. */
1278 int_cst_node = make_node (INTEGER_CST);
1282 return t;
1285 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1286 and the rest are zeros. */
1288 tree
1289 build_low_bits_mask (tree type, unsigned bits)
1291 double_int mask;
1293 gcc_assert (bits <= TYPE_PRECISION (type));
1295 if (bits == TYPE_PRECISION (type)
1296 && !TYPE_UNSIGNED (type))
1297 /* Sign extended all-ones mask. */
1298 mask = double_int_minus_one;
1299 else
1300 mask = double_int_mask (bits);
1302 return build_int_cst_wide (type, mask.low, mask.high);
1305 /* Checks that X is integer constant that can be expressed in (unsigned)
1306 HOST_WIDE_INT without loss of precision. */
1308 bool
1309 cst_and_fits_in_hwi (const_tree x)
1311 if (TREE_CODE (x) != INTEGER_CST)
1312 return false;
1314 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1315 return false;
1317 return (TREE_INT_CST_HIGH (x) == 0
1318 || TREE_INT_CST_HIGH (x) == -1);
1321 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1322 are in a list pointed to by VALS. */
1324 tree
1325 build_vector (tree type, tree vals)
1327 tree v = make_node (VECTOR_CST);
1328 int over = 0;
1329 tree link;
1330 unsigned cnt = 0;
1332 TREE_VECTOR_CST_ELTS (v) = vals;
1333 TREE_TYPE (v) = type;
1335 /* Iterate through elements and check for overflow. */
1336 for (link = vals; link; link = TREE_CHAIN (link))
1338 tree value = TREE_VALUE (link);
1339 cnt++;
1341 /* Don't crash if we get an address constant. */
1342 if (!CONSTANT_CLASS_P (value))
1343 continue;
1345 over |= TREE_OVERFLOW (value);
1348 gcc_assert (cnt == TYPE_VECTOR_SUBPARTS (type));
1350 TREE_OVERFLOW (v) = over;
1351 return v;
1354 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1355 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1357 tree
1358 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1360 tree list = NULL_TREE;
1361 unsigned HOST_WIDE_INT idx;
1362 tree value;
1364 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1365 list = tree_cons (NULL_TREE, value, list);
1366 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1367 list = tree_cons (NULL_TREE,
1368 build_zero_cst (TREE_TYPE (type)), list);
1369 return build_vector (type, nreverse (list));
1372 /* Build a vector of type VECTYPE where all the elements are SCs. */
1373 tree
1374 build_vector_from_val (tree vectype, tree sc)
1376 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1377 VEC(constructor_elt, gc) *v = NULL;
1379 if (sc == error_mark_node)
1380 return sc;
1382 /* Verify that the vector type is suitable for SC. Note that there
1383 is some inconsistency in the type-system with respect to restrict
1384 qualifications of pointers. Vector types always have a main-variant
1385 element type and the qualification is applied to the vector-type.
1386 So TREE_TYPE (vector-type) does not return a properly qualified
1387 vector element-type. */
1388 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1389 TREE_TYPE (vectype)));
1391 v = VEC_alloc (constructor_elt, gc, nunits);
1392 for (i = 0; i < nunits; ++i)
1393 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1395 if (CONSTANT_CLASS_P (sc))
1396 return build_vector_from_ctor (vectype, v);
1397 else
1398 return build_constructor (vectype, v);
1401 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1402 are in the VEC pointed to by VALS. */
1403 tree
1404 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1406 tree c = make_node (CONSTRUCTOR);
1407 unsigned int i;
1408 constructor_elt *elt;
1409 bool constant_p = true;
1411 TREE_TYPE (c) = type;
1412 CONSTRUCTOR_ELTS (c) = vals;
1414 FOR_EACH_VEC_ELT (constructor_elt, vals, i, elt)
1415 if (!TREE_CONSTANT (elt->value))
1417 constant_p = false;
1418 break;
1421 TREE_CONSTANT (c) = constant_p;
1423 return c;
1426 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1427 INDEX and VALUE. */
1428 tree
1429 build_constructor_single (tree type, tree index, tree value)
1431 VEC(constructor_elt,gc) *v;
1432 constructor_elt *elt;
1434 v = VEC_alloc (constructor_elt, gc, 1);
1435 elt = VEC_quick_push (constructor_elt, v, NULL);
1436 elt->index = index;
1437 elt->value = value;
1439 return build_constructor (type, v);
1443 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1444 are in a list pointed to by VALS. */
1445 tree
1446 build_constructor_from_list (tree type, tree vals)
1448 tree t;
1449 VEC(constructor_elt,gc) *v = NULL;
1451 if (vals)
1453 v = VEC_alloc (constructor_elt, gc, list_length (vals));
1454 for (t = vals; t; t = TREE_CHAIN (t))
1455 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1458 return build_constructor (type, v);
1461 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1463 tree
1464 build_fixed (tree type, FIXED_VALUE_TYPE f)
1466 tree v;
1467 FIXED_VALUE_TYPE *fp;
1469 v = make_node (FIXED_CST);
1470 fp = ggc_alloc_fixed_value ();
1471 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1473 TREE_TYPE (v) = type;
1474 TREE_FIXED_CST_PTR (v) = fp;
1475 return v;
1478 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1480 tree
1481 build_real (tree type, REAL_VALUE_TYPE d)
1483 tree v;
1484 REAL_VALUE_TYPE *dp;
1485 int overflow = 0;
1487 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1488 Consider doing it via real_convert now. */
1490 v = make_node (REAL_CST);
1491 dp = ggc_alloc_real_value ();
1492 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1494 TREE_TYPE (v) = type;
1495 TREE_REAL_CST_PTR (v) = dp;
1496 TREE_OVERFLOW (v) = overflow;
1497 return v;
1500 /* Return a new REAL_CST node whose type is TYPE
1501 and whose value is the integer value of the INTEGER_CST node I. */
1503 REAL_VALUE_TYPE
1504 real_value_from_int_cst (const_tree type, const_tree i)
1506 REAL_VALUE_TYPE d;
1508 /* Clear all bits of the real value type so that we can later do
1509 bitwise comparisons to see if two values are the same. */
1510 memset (&d, 0, sizeof d);
1512 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1513 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1514 TYPE_UNSIGNED (TREE_TYPE (i)));
1515 return d;
1518 /* Given a tree representing an integer constant I, return a tree
1519 representing the same value as a floating-point constant of type TYPE. */
1521 tree
1522 build_real_from_int_cst (tree type, const_tree i)
1524 tree v;
1525 int overflow = TREE_OVERFLOW (i);
1527 v = build_real (type, real_value_from_int_cst (type, i));
1529 TREE_OVERFLOW (v) |= overflow;
1530 return v;
1533 /* Return a newly constructed STRING_CST node whose value is
1534 the LEN characters at STR.
1535 The TREE_TYPE is not initialized. */
1537 tree
1538 build_string (int len, const char *str)
1540 tree s;
1541 size_t length;
1543 /* Do not waste bytes provided by padding of struct tree_string. */
1544 length = len + offsetof (struct tree_string, str) + 1;
1546 record_node_allocation_statistics (STRING_CST, length);
1548 s = ggc_alloc_tree_node (length);
1550 memset (s, 0, sizeof (struct tree_typed));
1551 TREE_SET_CODE (s, STRING_CST);
1552 TREE_CONSTANT (s) = 1;
1553 TREE_STRING_LENGTH (s) = len;
1554 memcpy (s->string.str, str, len);
1555 s->string.str[len] = '\0';
1557 return s;
1560 /* Return a newly constructed COMPLEX_CST node whose value is
1561 specified by the real and imaginary parts REAL and IMAG.
1562 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1563 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1565 tree
1566 build_complex (tree type, tree real, tree imag)
1568 tree t = make_node (COMPLEX_CST);
1570 TREE_REALPART (t) = real;
1571 TREE_IMAGPART (t) = imag;
1572 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1573 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1574 return t;
1577 /* Return a constant of arithmetic type TYPE which is the
1578 multiplicative identity of the set TYPE. */
1580 tree
1581 build_one_cst (tree type)
1583 switch (TREE_CODE (type))
1585 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1586 case POINTER_TYPE: case REFERENCE_TYPE:
1587 case OFFSET_TYPE:
1588 return build_int_cst (type, 1);
1590 case REAL_TYPE:
1591 return build_real (type, dconst1);
1593 case FIXED_POINT_TYPE:
1594 /* We can only generate 1 for accum types. */
1595 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1596 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1598 case VECTOR_TYPE:
1600 tree scalar = build_one_cst (TREE_TYPE (type));
1602 return build_vector_from_val (type, scalar);
1605 case COMPLEX_TYPE:
1606 return build_complex (type,
1607 build_one_cst (TREE_TYPE (type)),
1608 build_zero_cst (TREE_TYPE (type)));
1610 default:
1611 gcc_unreachable ();
1615 /* Build 0 constant of type TYPE. This is used by constructor folding
1616 and thus the constant should be represented in memory by
1617 zero(es). */
1619 tree
1620 build_zero_cst (tree type)
1622 switch (TREE_CODE (type))
1624 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1625 case POINTER_TYPE: case REFERENCE_TYPE:
1626 case OFFSET_TYPE:
1627 return build_int_cst (type, 0);
1629 case REAL_TYPE:
1630 return build_real (type, dconst0);
1632 case FIXED_POINT_TYPE:
1633 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1635 case VECTOR_TYPE:
1637 tree scalar = build_zero_cst (TREE_TYPE (type));
1639 return build_vector_from_val (type, scalar);
1642 case COMPLEX_TYPE:
1644 tree zero = build_zero_cst (TREE_TYPE (type));
1646 return build_complex (type, zero, zero);
1649 default:
1650 if (!AGGREGATE_TYPE_P (type))
1651 return fold_convert (type, integer_zero_node);
1652 return build_constructor (type, NULL);
1657 /* Build a BINFO with LEN language slots. */
1659 tree
1660 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1662 tree t;
1663 size_t length = (offsetof (struct tree_binfo, base_binfos)
1664 + VEC_embedded_size (tree, base_binfos));
1666 record_node_allocation_statistics (TREE_BINFO, length);
1668 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1670 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1672 TREE_SET_CODE (t, TREE_BINFO);
1674 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1676 return t;
1679 /* Create a CASE_LABEL_EXPR tree node and return it. */
1681 tree
1682 build_case_label (tree low_value, tree high_value, tree label_decl)
1684 tree t = make_node (CASE_LABEL_EXPR);
1686 TREE_TYPE (t) = void_type_node;
1687 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1689 CASE_LOW (t) = low_value;
1690 CASE_HIGH (t) = high_value;
1691 CASE_LABEL (t) = label_decl;
1692 CASE_CHAIN (t) = NULL_TREE;
1694 return t;
1697 /* Build a newly constructed TREE_VEC node of length LEN. */
1699 tree
1700 make_tree_vec_stat (int len MEM_STAT_DECL)
1702 tree t;
1703 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1705 record_node_allocation_statistics (TREE_VEC, length);
1707 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
1709 TREE_SET_CODE (t, TREE_VEC);
1710 TREE_VEC_LENGTH (t) = len;
1712 return t;
1715 /* Return 1 if EXPR is the integer constant zero or a complex constant
1716 of zero. */
1719 integer_zerop (const_tree expr)
1721 STRIP_NOPS (expr);
1723 return ((TREE_CODE (expr) == INTEGER_CST
1724 && TREE_INT_CST_LOW (expr) == 0
1725 && TREE_INT_CST_HIGH (expr) == 0)
1726 || (TREE_CODE (expr) == COMPLEX_CST
1727 && integer_zerop (TREE_REALPART (expr))
1728 && integer_zerop (TREE_IMAGPART (expr))));
1731 /* Return 1 if EXPR is the integer constant one or the corresponding
1732 complex constant. */
1735 integer_onep (const_tree expr)
1737 STRIP_NOPS (expr);
1739 return ((TREE_CODE (expr) == INTEGER_CST
1740 && TREE_INT_CST_LOW (expr) == 1
1741 && TREE_INT_CST_HIGH (expr) == 0)
1742 || (TREE_CODE (expr) == COMPLEX_CST
1743 && integer_onep (TREE_REALPART (expr))
1744 && integer_zerop (TREE_IMAGPART (expr))));
1747 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1748 it contains. Likewise for the corresponding complex constant. */
1751 integer_all_onesp (const_tree expr)
1753 int prec;
1754 int uns;
1756 STRIP_NOPS (expr);
1758 if (TREE_CODE (expr) == COMPLEX_CST
1759 && integer_all_onesp (TREE_REALPART (expr))
1760 && integer_zerop (TREE_IMAGPART (expr)))
1761 return 1;
1763 else if (TREE_CODE (expr) != INTEGER_CST)
1764 return 0;
1766 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1767 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1768 && TREE_INT_CST_HIGH (expr) == -1)
1769 return 1;
1770 if (!uns)
1771 return 0;
1773 /* Note that using TYPE_PRECISION here is wrong. We care about the
1774 actual bits, not the (arbitrary) range of the type. */
1775 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1776 if (prec >= HOST_BITS_PER_WIDE_INT)
1778 HOST_WIDE_INT high_value;
1779 int shift_amount;
1781 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1783 /* Can not handle precisions greater than twice the host int size. */
1784 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1785 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1786 /* Shifting by the host word size is undefined according to the ANSI
1787 standard, so we must handle this as a special case. */
1788 high_value = -1;
1789 else
1790 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1792 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1793 && TREE_INT_CST_HIGH (expr) == high_value);
1795 else
1796 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1799 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1800 one bit on). */
1803 integer_pow2p (const_tree expr)
1805 int prec;
1806 HOST_WIDE_INT high, low;
1808 STRIP_NOPS (expr);
1810 if (TREE_CODE (expr) == COMPLEX_CST
1811 && integer_pow2p (TREE_REALPART (expr))
1812 && integer_zerop (TREE_IMAGPART (expr)))
1813 return 1;
1815 if (TREE_CODE (expr) != INTEGER_CST)
1816 return 0;
1818 prec = TYPE_PRECISION (TREE_TYPE (expr));
1819 high = TREE_INT_CST_HIGH (expr);
1820 low = TREE_INT_CST_LOW (expr);
1822 /* First clear all bits that are beyond the type's precision in case
1823 we've been sign extended. */
1825 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1827 else if (prec > HOST_BITS_PER_WIDE_INT)
1828 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1829 else
1831 high = 0;
1832 if (prec < HOST_BITS_PER_WIDE_INT)
1833 low &= ~((HOST_WIDE_INT) (-1) << prec);
1836 if (high == 0 && low == 0)
1837 return 0;
1839 return ((high == 0 && (low & (low - 1)) == 0)
1840 || (low == 0 && (high & (high - 1)) == 0));
1843 /* Return 1 if EXPR is an integer constant other than zero or a
1844 complex constant other than zero. */
1847 integer_nonzerop (const_tree expr)
1849 STRIP_NOPS (expr);
1851 return ((TREE_CODE (expr) == INTEGER_CST
1852 && (TREE_INT_CST_LOW (expr) != 0
1853 || TREE_INT_CST_HIGH (expr) != 0))
1854 || (TREE_CODE (expr) == COMPLEX_CST
1855 && (integer_nonzerop (TREE_REALPART (expr))
1856 || integer_nonzerop (TREE_IMAGPART (expr)))));
1859 /* Return 1 if EXPR is the fixed-point constant zero. */
1862 fixed_zerop (const_tree expr)
1864 return (TREE_CODE (expr) == FIXED_CST
1865 && double_int_zero_p (TREE_FIXED_CST (expr).data));
1868 /* Return the power of two represented by a tree node known to be a
1869 power of two. */
1872 tree_log2 (const_tree expr)
1874 int prec;
1875 HOST_WIDE_INT high, low;
1877 STRIP_NOPS (expr);
1879 if (TREE_CODE (expr) == COMPLEX_CST)
1880 return tree_log2 (TREE_REALPART (expr));
1882 prec = TYPE_PRECISION (TREE_TYPE (expr));
1883 high = TREE_INT_CST_HIGH (expr);
1884 low = TREE_INT_CST_LOW (expr);
1886 /* First clear all bits that are beyond the type's precision in case
1887 we've been sign extended. */
1889 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1891 else if (prec > HOST_BITS_PER_WIDE_INT)
1892 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1893 else
1895 high = 0;
1896 if (prec < HOST_BITS_PER_WIDE_INT)
1897 low &= ~((HOST_WIDE_INT) (-1) << prec);
1900 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1901 : exact_log2 (low));
1904 /* Similar, but return the largest integer Y such that 2 ** Y is less
1905 than or equal to EXPR. */
1908 tree_floor_log2 (const_tree expr)
1910 int prec;
1911 HOST_WIDE_INT high, low;
1913 STRIP_NOPS (expr);
1915 if (TREE_CODE (expr) == COMPLEX_CST)
1916 return tree_log2 (TREE_REALPART (expr));
1918 prec = TYPE_PRECISION (TREE_TYPE (expr));
1919 high = TREE_INT_CST_HIGH (expr);
1920 low = TREE_INT_CST_LOW (expr);
1922 /* First clear all bits that are beyond the type's precision in case
1923 we've been sign extended. Ignore if type's precision hasn't been set
1924 since what we are doing is setting it. */
1926 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1928 else if (prec > HOST_BITS_PER_WIDE_INT)
1929 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1930 else
1932 high = 0;
1933 if (prec < HOST_BITS_PER_WIDE_INT)
1934 low &= ~((HOST_WIDE_INT) (-1) << prec);
1937 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1938 : floor_log2 (low));
1941 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
1942 decimal float constants, so don't return 1 for them. */
1945 real_zerop (const_tree expr)
1947 STRIP_NOPS (expr);
1949 return ((TREE_CODE (expr) == REAL_CST
1950 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
1951 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr)))))
1952 || (TREE_CODE (expr) == COMPLEX_CST
1953 && real_zerop (TREE_REALPART (expr))
1954 && real_zerop (TREE_IMAGPART (expr))));
1957 /* Return 1 if EXPR is the real constant one in real or complex form.
1958 Trailing zeroes matter for decimal float constants, so don't return
1959 1 for them. */
1962 real_onep (const_tree expr)
1964 STRIP_NOPS (expr);
1966 return ((TREE_CODE (expr) == REAL_CST
1967 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
1968 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr)))))
1969 || (TREE_CODE (expr) == COMPLEX_CST
1970 && real_onep (TREE_REALPART (expr))
1971 && real_zerop (TREE_IMAGPART (expr))));
1974 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
1975 for decimal float constants, so don't return 1 for them. */
1978 real_twop (const_tree expr)
1980 STRIP_NOPS (expr);
1982 return ((TREE_CODE (expr) == REAL_CST
1983 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
1984 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr)))))
1985 || (TREE_CODE (expr) == COMPLEX_CST
1986 && real_twop (TREE_REALPART (expr))
1987 && real_zerop (TREE_IMAGPART (expr))));
1990 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
1991 matter for decimal float constants, so don't return 1 for them. */
1994 real_minus_onep (const_tree expr)
1996 STRIP_NOPS (expr);
1998 return ((TREE_CODE (expr) == REAL_CST
1999 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2000 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr)))))
2001 || (TREE_CODE (expr) == COMPLEX_CST
2002 && real_minus_onep (TREE_REALPART (expr))
2003 && real_zerop (TREE_IMAGPART (expr))));
2006 /* Nonzero if EXP is a constant or a cast of a constant. */
2009 really_constant_p (const_tree exp)
2011 /* This is not quite the same as STRIP_NOPS. It does more. */
2012 while (CONVERT_EXPR_P (exp)
2013 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2014 exp = TREE_OPERAND (exp, 0);
2015 return TREE_CONSTANT (exp);
2018 /* Return first list element whose TREE_VALUE is ELEM.
2019 Return 0 if ELEM is not in LIST. */
2021 tree
2022 value_member (tree elem, tree list)
2024 while (list)
2026 if (elem == TREE_VALUE (list))
2027 return list;
2028 list = TREE_CHAIN (list);
2030 return NULL_TREE;
2033 /* Return first list element whose TREE_PURPOSE is ELEM.
2034 Return 0 if ELEM is not in LIST. */
2036 tree
2037 purpose_member (const_tree elem, tree list)
2039 while (list)
2041 if (elem == TREE_PURPOSE (list))
2042 return list;
2043 list = TREE_CHAIN (list);
2045 return NULL_TREE;
2048 /* Return true if ELEM is in V. */
2050 bool
2051 vec_member (const_tree elem, VEC(tree,gc) *v)
2053 unsigned ix;
2054 tree t;
2055 FOR_EACH_VEC_ELT (tree, v, ix, t)
2056 if (elem == t)
2057 return true;
2058 return false;
2061 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2062 NULL_TREE. */
2064 tree
2065 chain_index (int idx, tree chain)
2067 for (; chain && idx > 0; --idx)
2068 chain = TREE_CHAIN (chain);
2069 return chain;
2072 /* Return nonzero if ELEM is part of the chain CHAIN. */
2075 chain_member (const_tree elem, const_tree chain)
2077 while (chain)
2079 if (elem == chain)
2080 return 1;
2081 chain = DECL_CHAIN (chain);
2084 return 0;
2087 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2088 We expect a null pointer to mark the end of the chain.
2089 This is the Lisp primitive `length'. */
2092 list_length (const_tree t)
2094 const_tree p = t;
2095 #ifdef ENABLE_TREE_CHECKING
2096 const_tree q = t;
2097 #endif
2098 int len = 0;
2100 while (p)
2102 p = TREE_CHAIN (p);
2103 #ifdef ENABLE_TREE_CHECKING
2104 if (len % 2)
2105 q = TREE_CHAIN (q);
2106 gcc_assert (p != q);
2107 #endif
2108 len++;
2111 return len;
2114 /* Returns the number of FIELD_DECLs in TYPE. */
2117 fields_length (const_tree type)
2119 tree t = TYPE_FIELDS (type);
2120 int count = 0;
2122 for (; t; t = DECL_CHAIN (t))
2123 if (TREE_CODE (t) == FIELD_DECL)
2124 ++count;
2126 return count;
2129 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2130 UNION_TYPE TYPE, or NULL_TREE if none. */
2132 tree
2133 first_field (const_tree type)
2135 tree t = TYPE_FIELDS (type);
2136 while (t && TREE_CODE (t) != FIELD_DECL)
2137 t = TREE_CHAIN (t);
2138 return t;
2141 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2142 by modifying the last node in chain 1 to point to chain 2.
2143 This is the Lisp primitive `nconc'. */
2145 tree
2146 chainon (tree op1, tree op2)
2148 tree t1;
2150 if (!op1)
2151 return op2;
2152 if (!op2)
2153 return op1;
2155 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2156 continue;
2157 TREE_CHAIN (t1) = op2;
2159 #ifdef ENABLE_TREE_CHECKING
2161 tree t2;
2162 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2163 gcc_assert (t2 != t1);
2165 #endif
2167 return op1;
2170 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2172 tree
2173 tree_last (tree chain)
2175 tree next;
2176 if (chain)
2177 while ((next = TREE_CHAIN (chain)))
2178 chain = next;
2179 return chain;
2182 /* Reverse the order of elements in the chain T,
2183 and return the new head of the chain (old last element). */
2185 tree
2186 nreverse (tree t)
2188 tree prev = 0, decl, next;
2189 for (decl = t; decl; decl = next)
2191 /* We shouldn't be using this function to reverse BLOCK chains; we
2192 have blocks_nreverse for that. */
2193 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2194 next = TREE_CHAIN (decl);
2195 TREE_CHAIN (decl) = prev;
2196 prev = decl;
2198 return prev;
2201 /* Return a newly created TREE_LIST node whose
2202 purpose and value fields are PARM and VALUE. */
2204 tree
2205 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2207 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2208 TREE_PURPOSE (t) = parm;
2209 TREE_VALUE (t) = value;
2210 return t;
2213 /* Build a chain of TREE_LIST nodes from a vector. */
2215 tree
2216 build_tree_list_vec_stat (const VEC(tree,gc) *vec MEM_STAT_DECL)
2218 tree ret = NULL_TREE;
2219 tree *pp = &ret;
2220 unsigned int i;
2221 tree t;
2222 FOR_EACH_VEC_ELT (tree, vec, i, t)
2224 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2225 pp = &TREE_CHAIN (*pp);
2227 return ret;
2230 /* Return a newly created TREE_LIST node whose
2231 purpose and value fields are PURPOSE and VALUE
2232 and whose TREE_CHAIN is CHAIN. */
2234 tree
2235 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2237 tree node;
2239 node = ggc_alloc_zone_tree_node_stat (&tree_zone, sizeof (struct tree_list)
2240 PASS_MEM_STAT);
2241 memset (node, 0, sizeof (struct tree_common));
2243 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2245 TREE_SET_CODE (node, TREE_LIST);
2246 TREE_CHAIN (node) = chain;
2247 TREE_PURPOSE (node) = purpose;
2248 TREE_VALUE (node) = value;
2249 return node;
2252 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2253 trees. */
2255 VEC(tree,gc) *
2256 ctor_to_vec (tree ctor)
2258 VEC(tree, gc) *vec = VEC_alloc (tree, gc, CONSTRUCTOR_NELTS (ctor));
2259 unsigned int ix;
2260 tree val;
2262 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2263 VEC_quick_push (tree, vec, val);
2265 return vec;
2268 /* Return the size nominally occupied by an object of type TYPE
2269 when it resides in memory. The value is measured in units of bytes,
2270 and its data type is that normally used for type sizes
2271 (which is the first type created by make_signed_type or
2272 make_unsigned_type). */
2274 tree
2275 size_in_bytes (const_tree type)
2277 tree t;
2279 if (type == error_mark_node)
2280 return integer_zero_node;
2282 type = TYPE_MAIN_VARIANT (type);
2283 t = TYPE_SIZE_UNIT (type);
2285 if (t == 0)
2287 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2288 return size_zero_node;
2291 return t;
2294 /* Return the size of TYPE (in bytes) as a wide integer
2295 or return -1 if the size can vary or is larger than an integer. */
2297 HOST_WIDE_INT
2298 int_size_in_bytes (const_tree type)
2300 tree t;
2302 if (type == error_mark_node)
2303 return 0;
2305 type = TYPE_MAIN_VARIANT (type);
2306 t = TYPE_SIZE_UNIT (type);
2307 if (t == 0
2308 || TREE_CODE (t) != INTEGER_CST
2309 || TREE_INT_CST_HIGH (t) != 0
2310 /* If the result would appear negative, it's too big to represent. */
2311 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2312 return -1;
2314 return TREE_INT_CST_LOW (t);
2317 /* Return the maximum size of TYPE (in bytes) as a wide integer
2318 or return -1 if the size can vary or is larger than an integer. */
2320 HOST_WIDE_INT
2321 max_int_size_in_bytes (const_tree type)
2323 HOST_WIDE_INT size = -1;
2324 tree size_tree;
2326 /* If this is an array type, check for a possible MAX_SIZE attached. */
2328 if (TREE_CODE (type) == ARRAY_TYPE)
2330 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2332 if (size_tree && host_integerp (size_tree, 1))
2333 size = tree_low_cst (size_tree, 1);
2336 /* If we still haven't been able to get a size, see if the language
2337 can compute a maximum size. */
2339 if (size == -1)
2341 size_tree = lang_hooks.types.max_size (type);
2343 if (size_tree && host_integerp (size_tree, 1))
2344 size = tree_low_cst (size_tree, 1);
2347 return size;
2350 /* Returns a tree for the size of EXP in bytes. */
2352 tree
2353 tree_expr_size (const_tree exp)
2355 if (DECL_P (exp)
2356 && DECL_SIZE_UNIT (exp) != 0)
2357 return DECL_SIZE_UNIT (exp);
2358 else
2359 return size_in_bytes (TREE_TYPE (exp));
2362 /* Return the bit position of FIELD, in bits from the start of the record.
2363 This is a tree of type bitsizetype. */
2365 tree
2366 bit_position (const_tree field)
2368 return bit_from_pos (DECL_FIELD_OFFSET (field),
2369 DECL_FIELD_BIT_OFFSET (field));
2372 /* Likewise, but return as an integer. It must be representable in
2373 that way (since it could be a signed value, we don't have the
2374 option of returning -1 like int_size_in_byte can. */
2376 HOST_WIDE_INT
2377 int_bit_position (const_tree field)
2379 return tree_low_cst (bit_position (field), 0);
2382 /* Return the byte position of FIELD, in bytes from the start of the record.
2383 This is a tree of type sizetype. */
2385 tree
2386 byte_position (const_tree field)
2388 return byte_from_pos (DECL_FIELD_OFFSET (field),
2389 DECL_FIELD_BIT_OFFSET (field));
2392 /* Likewise, but return as an integer. It must be representable in
2393 that way (since it could be a signed value, we don't have the
2394 option of returning -1 like int_size_in_byte can. */
2396 HOST_WIDE_INT
2397 int_byte_position (const_tree field)
2399 return tree_low_cst (byte_position (field), 0);
2402 /* Return the strictest alignment, in bits, that T is known to have. */
2404 unsigned int
2405 expr_align (const_tree t)
2407 unsigned int align0, align1;
2409 switch (TREE_CODE (t))
2411 CASE_CONVERT: case NON_LVALUE_EXPR:
2412 /* If we have conversions, we know that the alignment of the
2413 object must meet each of the alignments of the types. */
2414 align0 = expr_align (TREE_OPERAND (t, 0));
2415 align1 = TYPE_ALIGN (TREE_TYPE (t));
2416 return MAX (align0, align1);
2418 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2419 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2420 case CLEANUP_POINT_EXPR:
2421 /* These don't change the alignment of an object. */
2422 return expr_align (TREE_OPERAND (t, 0));
2424 case COND_EXPR:
2425 /* The best we can do is say that the alignment is the least aligned
2426 of the two arms. */
2427 align0 = expr_align (TREE_OPERAND (t, 1));
2428 align1 = expr_align (TREE_OPERAND (t, 2));
2429 return MIN (align0, align1);
2431 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2432 meaningfully, it's always 1. */
2433 case LABEL_DECL: case CONST_DECL:
2434 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2435 case FUNCTION_DECL:
2436 gcc_assert (DECL_ALIGN (t) != 0);
2437 return DECL_ALIGN (t);
2439 default:
2440 break;
2443 /* Otherwise take the alignment from that of the type. */
2444 return TYPE_ALIGN (TREE_TYPE (t));
2447 /* Return, as a tree node, the number of elements for TYPE (which is an
2448 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2450 tree
2451 array_type_nelts (const_tree type)
2453 tree index_type, min, max;
2455 /* If they did it with unspecified bounds, then we should have already
2456 given an error about it before we got here. */
2457 if (! TYPE_DOMAIN (type))
2458 return error_mark_node;
2460 index_type = TYPE_DOMAIN (type);
2461 min = TYPE_MIN_VALUE (index_type);
2462 max = TYPE_MAX_VALUE (index_type);
2464 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2465 if (!max)
2466 return error_mark_node;
2468 return (integer_zerop (min)
2469 ? max
2470 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2473 /* If arg is static -- a reference to an object in static storage -- then
2474 return the object. This is not the same as the C meaning of `static'.
2475 If arg isn't static, return NULL. */
2477 tree
2478 staticp (tree arg)
2480 switch (TREE_CODE (arg))
2482 case FUNCTION_DECL:
2483 /* Nested functions are static, even though taking their address will
2484 involve a trampoline as we unnest the nested function and create
2485 the trampoline on the tree level. */
2486 return arg;
2488 case VAR_DECL:
2489 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2490 && ! DECL_THREAD_LOCAL_P (arg)
2491 && ! DECL_DLLIMPORT_P (arg)
2492 ? arg : NULL);
2494 case CONST_DECL:
2495 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2496 ? arg : NULL);
2498 case CONSTRUCTOR:
2499 return TREE_STATIC (arg) ? arg : NULL;
2501 case LABEL_DECL:
2502 case STRING_CST:
2503 return arg;
2505 case COMPONENT_REF:
2506 /* If the thing being referenced is not a field, then it is
2507 something language specific. */
2508 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2510 /* If we are referencing a bitfield, we can't evaluate an
2511 ADDR_EXPR at compile time and so it isn't a constant. */
2512 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2513 return NULL;
2515 return staticp (TREE_OPERAND (arg, 0));
2517 case BIT_FIELD_REF:
2518 return NULL;
2520 case INDIRECT_REF:
2521 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2523 case ARRAY_REF:
2524 case ARRAY_RANGE_REF:
2525 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2526 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2527 return staticp (TREE_OPERAND (arg, 0));
2528 else
2529 return NULL;
2531 case COMPOUND_LITERAL_EXPR:
2532 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2534 default:
2535 return NULL;
2542 /* Return whether OP is a DECL whose address is function-invariant. */
2544 bool
2545 decl_address_invariant_p (const_tree op)
2547 /* The conditions below are slightly less strict than the one in
2548 staticp. */
2550 switch (TREE_CODE (op))
2552 case PARM_DECL:
2553 case RESULT_DECL:
2554 case LABEL_DECL:
2555 case FUNCTION_DECL:
2556 return true;
2558 case VAR_DECL:
2559 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2560 || DECL_THREAD_LOCAL_P (op)
2561 || DECL_CONTEXT (op) == current_function_decl
2562 || decl_function_context (op) == current_function_decl)
2563 return true;
2564 break;
2566 case CONST_DECL:
2567 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2568 || decl_function_context (op) == current_function_decl)
2569 return true;
2570 break;
2572 default:
2573 break;
2576 return false;
2579 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2581 bool
2582 decl_address_ip_invariant_p (const_tree op)
2584 /* The conditions below are slightly less strict than the one in
2585 staticp. */
2587 switch (TREE_CODE (op))
2589 case LABEL_DECL:
2590 case FUNCTION_DECL:
2591 case STRING_CST:
2592 return true;
2594 case VAR_DECL:
2595 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2596 && !DECL_DLLIMPORT_P (op))
2597 || DECL_THREAD_LOCAL_P (op))
2598 return true;
2599 break;
2601 case CONST_DECL:
2602 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2603 return true;
2604 break;
2606 default:
2607 break;
2610 return false;
2614 /* Return true if T is function-invariant (internal function, does
2615 not handle arithmetic; that's handled in skip_simple_arithmetic and
2616 tree_invariant_p). */
2618 static bool tree_invariant_p (tree t);
2620 static bool
2621 tree_invariant_p_1 (tree t)
2623 tree op;
2625 if (TREE_CONSTANT (t)
2626 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2627 return true;
2629 switch (TREE_CODE (t))
2631 case SAVE_EXPR:
2632 return true;
2634 case ADDR_EXPR:
2635 op = TREE_OPERAND (t, 0);
2636 while (handled_component_p (op))
2638 switch (TREE_CODE (op))
2640 case ARRAY_REF:
2641 case ARRAY_RANGE_REF:
2642 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2643 || TREE_OPERAND (op, 2) != NULL_TREE
2644 || TREE_OPERAND (op, 3) != NULL_TREE)
2645 return false;
2646 break;
2648 case COMPONENT_REF:
2649 if (TREE_OPERAND (op, 2) != NULL_TREE)
2650 return false;
2651 break;
2653 default:;
2655 op = TREE_OPERAND (op, 0);
2658 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2660 default:
2661 break;
2664 return false;
2667 /* Return true if T is function-invariant. */
2669 static bool
2670 tree_invariant_p (tree t)
2672 tree inner = skip_simple_arithmetic (t);
2673 return tree_invariant_p_1 (inner);
2676 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2677 Do this to any expression which may be used in more than one place,
2678 but must be evaluated only once.
2680 Normally, expand_expr would reevaluate the expression each time.
2681 Calling save_expr produces something that is evaluated and recorded
2682 the first time expand_expr is called on it. Subsequent calls to
2683 expand_expr just reuse the recorded value.
2685 The call to expand_expr that generates code that actually computes
2686 the value is the first call *at compile time*. Subsequent calls
2687 *at compile time* generate code to use the saved value.
2688 This produces correct result provided that *at run time* control
2689 always flows through the insns made by the first expand_expr
2690 before reaching the other places where the save_expr was evaluated.
2691 You, the caller of save_expr, must make sure this is so.
2693 Constants, and certain read-only nodes, are returned with no
2694 SAVE_EXPR because that is safe. Expressions containing placeholders
2695 are not touched; see tree.def for an explanation of what these
2696 are used for. */
2698 tree
2699 save_expr (tree expr)
2701 tree t = fold (expr);
2702 tree inner;
2704 /* If the tree evaluates to a constant, then we don't want to hide that
2705 fact (i.e. this allows further folding, and direct checks for constants).
2706 However, a read-only object that has side effects cannot be bypassed.
2707 Since it is no problem to reevaluate literals, we just return the
2708 literal node. */
2709 inner = skip_simple_arithmetic (t);
2710 if (TREE_CODE (inner) == ERROR_MARK)
2711 return inner;
2713 if (tree_invariant_p_1 (inner))
2714 return t;
2716 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2717 it means that the size or offset of some field of an object depends on
2718 the value within another field.
2720 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2721 and some variable since it would then need to be both evaluated once and
2722 evaluated more than once. Front-ends must assure this case cannot
2723 happen by surrounding any such subexpressions in their own SAVE_EXPR
2724 and forcing evaluation at the proper time. */
2725 if (contains_placeholder_p (inner))
2726 return t;
2728 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2729 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2731 /* This expression might be placed ahead of a jump to ensure that the
2732 value was computed on both sides of the jump. So make sure it isn't
2733 eliminated as dead. */
2734 TREE_SIDE_EFFECTS (t) = 1;
2735 return t;
2738 /* Look inside EXPR and into any simple arithmetic operations. Return
2739 the innermost non-arithmetic node. */
2741 tree
2742 skip_simple_arithmetic (tree expr)
2744 tree inner;
2746 /* We don't care about whether this can be used as an lvalue in this
2747 context. */
2748 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2749 expr = TREE_OPERAND (expr, 0);
2751 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2752 a constant, it will be more efficient to not make another SAVE_EXPR since
2753 it will allow better simplification and GCSE will be able to merge the
2754 computations if they actually occur. */
2755 inner = expr;
2756 while (1)
2758 if (UNARY_CLASS_P (inner))
2759 inner = TREE_OPERAND (inner, 0);
2760 else if (BINARY_CLASS_P (inner))
2762 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2763 inner = TREE_OPERAND (inner, 0);
2764 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2765 inner = TREE_OPERAND (inner, 1);
2766 else
2767 break;
2769 else
2770 break;
2773 return inner;
2777 /* Return which tree structure is used by T. */
2779 enum tree_node_structure_enum
2780 tree_node_structure (const_tree t)
2782 const enum tree_code code = TREE_CODE (t);
2783 return tree_node_structure_for_code (code);
2786 /* Set various status flags when building a CALL_EXPR object T. */
2788 static void
2789 process_call_operands (tree t)
2791 bool side_effects = TREE_SIDE_EFFECTS (t);
2792 bool read_only = false;
2793 int i = call_expr_flags (t);
2795 /* Calls have side-effects, except those to const or pure functions. */
2796 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2797 side_effects = true;
2798 /* Propagate TREE_READONLY of arguments for const functions. */
2799 if (i & ECF_CONST)
2800 read_only = true;
2802 if (!side_effects || read_only)
2803 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2805 tree op = TREE_OPERAND (t, i);
2806 if (op && TREE_SIDE_EFFECTS (op))
2807 side_effects = true;
2808 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2809 read_only = false;
2812 TREE_SIDE_EFFECTS (t) = side_effects;
2813 TREE_READONLY (t) = read_only;
2816 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2817 size or offset that depends on a field within a record. */
2819 bool
2820 contains_placeholder_p (const_tree exp)
2822 enum tree_code code;
2824 if (!exp)
2825 return 0;
2827 code = TREE_CODE (exp);
2828 if (code == PLACEHOLDER_EXPR)
2829 return 1;
2831 switch (TREE_CODE_CLASS (code))
2833 case tcc_reference:
2834 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2835 position computations since they will be converted into a
2836 WITH_RECORD_EXPR involving the reference, which will assume
2837 here will be valid. */
2838 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2840 case tcc_exceptional:
2841 if (code == TREE_LIST)
2842 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2843 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2844 break;
2846 case tcc_unary:
2847 case tcc_binary:
2848 case tcc_comparison:
2849 case tcc_expression:
2850 switch (code)
2852 case COMPOUND_EXPR:
2853 /* Ignoring the first operand isn't quite right, but works best. */
2854 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2856 case COND_EXPR:
2857 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2858 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2859 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2861 case SAVE_EXPR:
2862 /* The save_expr function never wraps anything containing
2863 a PLACEHOLDER_EXPR. */
2864 return 0;
2866 default:
2867 break;
2870 switch (TREE_CODE_LENGTH (code))
2872 case 1:
2873 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2874 case 2:
2875 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2876 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2877 default:
2878 return 0;
2881 case tcc_vl_exp:
2882 switch (code)
2884 case CALL_EXPR:
2886 const_tree arg;
2887 const_call_expr_arg_iterator iter;
2888 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2889 if (CONTAINS_PLACEHOLDER_P (arg))
2890 return 1;
2891 return 0;
2893 default:
2894 return 0;
2897 default:
2898 return 0;
2900 return 0;
2903 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
2904 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
2905 field positions. */
2907 static bool
2908 type_contains_placeholder_1 (const_tree type)
2910 /* If the size contains a placeholder or the parent type (component type in
2911 the case of arrays) type involves a placeholder, this type does. */
2912 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2913 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2914 || (!POINTER_TYPE_P (type)
2915 && TREE_TYPE (type)
2916 && type_contains_placeholder_p (TREE_TYPE (type))))
2917 return true;
2919 /* Now do type-specific checks. Note that the last part of the check above
2920 greatly limits what we have to do below. */
2921 switch (TREE_CODE (type))
2923 case VOID_TYPE:
2924 case COMPLEX_TYPE:
2925 case ENUMERAL_TYPE:
2926 case BOOLEAN_TYPE:
2927 case POINTER_TYPE:
2928 case OFFSET_TYPE:
2929 case REFERENCE_TYPE:
2930 case METHOD_TYPE:
2931 case FUNCTION_TYPE:
2932 case VECTOR_TYPE:
2933 return false;
2935 case INTEGER_TYPE:
2936 case REAL_TYPE:
2937 case FIXED_POINT_TYPE:
2938 /* Here we just check the bounds. */
2939 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2940 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2942 case ARRAY_TYPE:
2943 /* We have already checked the component type above, so just check the
2944 domain type. */
2945 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2947 case RECORD_TYPE:
2948 case UNION_TYPE:
2949 case QUAL_UNION_TYPE:
2951 tree field;
2953 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2954 if (TREE_CODE (field) == FIELD_DECL
2955 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2956 || (TREE_CODE (type) == QUAL_UNION_TYPE
2957 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2958 || type_contains_placeholder_p (TREE_TYPE (field))))
2959 return true;
2961 return false;
2964 default:
2965 gcc_unreachable ();
2969 /* Wrapper around above function used to cache its result. */
2971 bool
2972 type_contains_placeholder_p (tree type)
2974 bool result;
2976 /* If the contains_placeholder_bits field has been initialized,
2977 then we know the answer. */
2978 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2979 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2981 /* Indicate that we've seen this type node, and the answer is false.
2982 This is what we want to return if we run into recursion via fields. */
2983 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2985 /* Compute the real value. */
2986 result = type_contains_placeholder_1 (type);
2988 /* Store the real value. */
2989 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2991 return result;
2994 /* Push tree EXP onto vector QUEUE if it is not already present. */
2996 static void
2997 push_without_duplicates (tree exp, VEC (tree, heap) **queue)
2999 unsigned int i;
3000 tree iter;
3002 FOR_EACH_VEC_ELT (tree, *queue, i, iter)
3003 if (simple_cst_equal (iter, exp) == 1)
3004 break;
3006 if (!iter)
3007 VEC_safe_push (tree, heap, *queue, exp);
3010 /* Given a tree EXP, find all occurences of references to fields
3011 in a PLACEHOLDER_EXPR and place them in vector REFS without
3012 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3013 we assume here that EXP contains only arithmetic expressions
3014 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3015 argument list. */
3017 void
3018 find_placeholder_in_expr (tree exp, VEC (tree, heap) **refs)
3020 enum tree_code code = TREE_CODE (exp);
3021 tree inner;
3022 int i;
3024 /* We handle TREE_LIST and COMPONENT_REF separately. */
3025 if (code == TREE_LIST)
3027 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3028 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3030 else if (code == COMPONENT_REF)
3032 for (inner = TREE_OPERAND (exp, 0);
3033 REFERENCE_CLASS_P (inner);
3034 inner = TREE_OPERAND (inner, 0))
3037 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3038 push_without_duplicates (exp, refs);
3039 else
3040 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3042 else
3043 switch (TREE_CODE_CLASS (code))
3045 case tcc_constant:
3046 break;
3048 case tcc_declaration:
3049 /* Variables allocated to static storage can stay. */
3050 if (!TREE_STATIC (exp))
3051 push_without_duplicates (exp, refs);
3052 break;
3054 case tcc_expression:
3055 /* This is the pattern built in ada/make_aligning_type. */
3056 if (code == ADDR_EXPR
3057 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3059 push_without_duplicates (exp, refs);
3060 break;
3063 /* Fall through... */
3065 case tcc_exceptional:
3066 case tcc_unary:
3067 case tcc_binary:
3068 case tcc_comparison:
3069 case tcc_reference:
3070 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3071 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3072 break;
3074 case tcc_vl_exp:
3075 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3076 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3077 break;
3079 default:
3080 gcc_unreachable ();
3084 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3085 return a tree with all occurrences of references to F in a
3086 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3087 CONST_DECLs. Note that we assume here that EXP contains only
3088 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3089 occurring only in their argument list. */
3091 tree
3092 substitute_in_expr (tree exp, tree f, tree r)
3094 enum tree_code code = TREE_CODE (exp);
3095 tree op0, op1, op2, op3;
3096 tree new_tree;
3098 /* We handle TREE_LIST and COMPONENT_REF separately. */
3099 if (code == TREE_LIST)
3101 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3102 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3103 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3104 return exp;
3106 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3108 else if (code == COMPONENT_REF)
3110 tree inner;
3112 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3113 and it is the right field, replace it with R. */
3114 for (inner = TREE_OPERAND (exp, 0);
3115 REFERENCE_CLASS_P (inner);
3116 inner = TREE_OPERAND (inner, 0))
3119 /* The field. */
3120 op1 = TREE_OPERAND (exp, 1);
3122 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3123 return r;
3125 /* If this expression hasn't been completed let, leave it alone. */
3126 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3127 return exp;
3129 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3130 if (op0 == TREE_OPERAND (exp, 0))
3131 return exp;
3133 new_tree
3134 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3136 else
3137 switch (TREE_CODE_CLASS (code))
3139 case tcc_constant:
3140 return exp;
3142 case tcc_declaration:
3143 if (exp == f)
3144 return r;
3145 else
3146 return exp;
3148 case tcc_expression:
3149 if (exp == f)
3150 return r;
3152 /* Fall through... */
3154 case tcc_exceptional:
3155 case tcc_unary:
3156 case tcc_binary:
3157 case tcc_comparison:
3158 case tcc_reference:
3159 switch (TREE_CODE_LENGTH (code))
3161 case 0:
3162 return exp;
3164 case 1:
3165 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3166 if (op0 == TREE_OPERAND (exp, 0))
3167 return exp;
3169 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3170 break;
3172 case 2:
3173 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3174 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3176 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3177 return exp;
3179 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3180 break;
3182 case 3:
3183 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3184 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3185 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3187 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3188 && op2 == TREE_OPERAND (exp, 2))
3189 return exp;
3191 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3192 break;
3194 case 4:
3195 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3196 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3197 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3198 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3200 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3201 && op2 == TREE_OPERAND (exp, 2)
3202 && op3 == TREE_OPERAND (exp, 3))
3203 return exp;
3205 new_tree
3206 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3207 break;
3209 default:
3210 gcc_unreachable ();
3212 break;
3214 case tcc_vl_exp:
3216 int i;
3218 new_tree = NULL_TREE;
3220 /* If we are trying to replace F with a constant, inline back
3221 functions which do nothing else than computing a value from
3222 the arguments they are passed. This makes it possible to
3223 fold partially or entirely the replacement expression. */
3224 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3226 tree t = maybe_inline_call_in_expr (exp);
3227 if (t)
3228 return SUBSTITUTE_IN_EXPR (t, f, r);
3231 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3233 tree op = TREE_OPERAND (exp, i);
3234 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3235 if (new_op != op)
3237 if (!new_tree)
3238 new_tree = copy_node (exp);
3239 TREE_OPERAND (new_tree, i) = new_op;
3243 if (new_tree)
3245 new_tree = fold (new_tree);
3246 if (TREE_CODE (new_tree) == CALL_EXPR)
3247 process_call_operands (new_tree);
3249 else
3250 return exp;
3252 break;
3254 default:
3255 gcc_unreachable ();
3258 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3260 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3261 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3263 return new_tree;
3266 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3267 for it within OBJ, a tree that is an object or a chain of references. */
3269 tree
3270 substitute_placeholder_in_expr (tree exp, tree obj)
3272 enum tree_code code = TREE_CODE (exp);
3273 tree op0, op1, op2, op3;
3274 tree new_tree;
3276 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3277 in the chain of OBJ. */
3278 if (code == PLACEHOLDER_EXPR)
3280 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3281 tree elt;
3283 for (elt = obj; elt != 0;
3284 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3285 || TREE_CODE (elt) == COND_EXPR)
3286 ? TREE_OPERAND (elt, 1)
3287 : (REFERENCE_CLASS_P (elt)
3288 || UNARY_CLASS_P (elt)
3289 || BINARY_CLASS_P (elt)
3290 || VL_EXP_CLASS_P (elt)
3291 || EXPRESSION_CLASS_P (elt))
3292 ? TREE_OPERAND (elt, 0) : 0))
3293 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3294 return elt;
3296 for (elt = obj; elt != 0;
3297 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3298 || TREE_CODE (elt) == COND_EXPR)
3299 ? TREE_OPERAND (elt, 1)
3300 : (REFERENCE_CLASS_P (elt)
3301 || UNARY_CLASS_P (elt)
3302 || BINARY_CLASS_P (elt)
3303 || VL_EXP_CLASS_P (elt)
3304 || EXPRESSION_CLASS_P (elt))
3305 ? TREE_OPERAND (elt, 0) : 0))
3306 if (POINTER_TYPE_P (TREE_TYPE (elt))
3307 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3308 == need_type))
3309 return fold_build1 (INDIRECT_REF, need_type, elt);
3311 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3312 survives until RTL generation, there will be an error. */
3313 return exp;
3316 /* TREE_LIST is special because we need to look at TREE_VALUE
3317 and TREE_CHAIN, not TREE_OPERANDS. */
3318 else if (code == TREE_LIST)
3320 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3321 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3322 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3323 return exp;
3325 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3327 else
3328 switch (TREE_CODE_CLASS (code))
3330 case tcc_constant:
3331 case tcc_declaration:
3332 return exp;
3334 case tcc_exceptional:
3335 case tcc_unary:
3336 case tcc_binary:
3337 case tcc_comparison:
3338 case tcc_expression:
3339 case tcc_reference:
3340 case tcc_statement:
3341 switch (TREE_CODE_LENGTH (code))
3343 case 0:
3344 return exp;
3346 case 1:
3347 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3348 if (op0 == TREE_OPERAND (exp, 0))
3349 return exp;
3351 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3352 break;
3354 case 2:
3355 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3356 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3358 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3359 return exp;
3361 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3362 break;
3364 case 3:
3365 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3366 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3367 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3369 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3370 && op2 == TREE_OPERAND (exp, 2))
3371 return exp;
3373 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3374 break;
3376 case 4:
3377 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3378 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3379 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3380 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3382 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3383 && op2 == TREE_OPERAND (exp, 2)
3384 && op3 == TREE_OPERAND (exp, 3))
3385 return exp;
3387 new_tree
3388 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3389 break;
3391 default:
3392 gcc_unreachable ();
3394 break;
3396 case tcc_vl_exp:
3398 int i;
3400 new_tree = NULL_TREE;
3402 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3404 tree op = TREE_OPERAND (exp, i);
3405 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3406 if (new_op != op)
3408 if (!new_tree)
3409 new_tree = copy_node (exp);
3410 TREE_OPERAND (new_tree, i) = new_op;
3414 if (new_tree)
3416 new_tree = fold (new_tree);
3417 if (TREE_CODE (new_tree) == CALL_EXPR)
3418 process_call_operands (new_tree);
3420 else
3421 return exp;
3423 break;
3425 default:
3426 gcc_unreachable ();
3429 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3431 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3432 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3434 return new_tree;
3437 /* Stabilize a reference so that we can use it any number of times
3438 without causing its operands to be evaluated more than once.
3439 Returns the stabilized reference. This works by means of save_expr,
3440 so see the caveats in the comments about save_expr.
3442 Also allows conversion expressions whose operands are references.
3443 Any other kind of expression is returned unchanged. */
3445 tree
3446 stabilize_reference (tree ref)
3448 tree result;
3449 enum tree_code code = TREE_CODE (ref);
3451 switch (code)
3453 case VAR_DECL:
3454 case PARM_DECL:
3455 case RESULT_DECL:
3456 /* No action is needed in this case. */
3457 return ref;
3459 CASE_CONVERT:
3460 case FLOAT_EXPR:
3461 case FIX_TRUNC_EXPR:
3462 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3463 break;
3465 case INDIRECT_REF:
3466 result = build_nt (INDIRECT_REF,
3467 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3468 break;
3470 case COMPONENT_REF:
3471 result = build_nt (COMPONENT_REF,
3472 stabilize_reference (TREE_OPERAND (ref, 0)),
3473 TREE_OPERAND (ref, 1), NULL_TREE);
3474 break;
3476 case BIT_FIELD_REF:
3477 result = build_nt (BIT_FIELD_REF,
3478 stabilize_reference (TREE_OPERAND (ref, 0)),
3479 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3480 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
3481 break;
3483 case ARRAY_REF:
3484 result = build_nt (ARRAY_REF,
3485 stabilize_reference (TREE_OPERAND (ref, 0)),
3486 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3487 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3488 break;
3490 case ARRAY_RANGE_REF:
3491 result = build_nt (ARRAY_RANGE_REF,
3492 stabilize_reference (TREE_OPERAND (ref, 0)),
3493 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3494 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3495 break;
3497 case COMPOUND_EXPR:
3498 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3499 it wouldn't be ignored. This matters when dealing with
3500 volatiles. */
3501 return stabilize_reference_1 (ref);
3503 /* If arg isn't a kind of lvalue we recognize, make no change.
3504 Caller should recognize the error for an invalid lvalue. */
3505 default:
3506 return ref;
3508 case ERROR_MARK:
3509 return error_mark_node;
3512 TREE_TYPE (result) = TREE_TYPE (ref);
3513 TREE_READONLY (result) = TREE_READONLY (ref);
3514 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3515 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3517 return result;
3520 /* Subroutine of stabilize_reference; this is called for subtrees of
3521 references. Any expression with side-effects must be put in a SAVE_EXPR
3522 to ensure that it is only evaluated once.
3524 We don't put SAVE_EXPR nodes around everything, because assigning very
3525 simple expressions to temporaries causes us to miss good opportunities
3526 for optimizations. Among other things, the opportunity to fold in the
3527 addition of a constant into an addressing mode often gets lost, e.g.
3528 "y[i+1] += x;". In general, we take the approach that we should not make
3529 an assignment unless we are forced into it - i.e., that any non-side effect
3530 operator should be allowed, and that cse should take care of coalescing
3531 multiple utterances of the same expression should that prove fruitful. */
3533 tree
3534 stabilize_reference_1 (tree e)
3536 tree result;
3537 enum tree_code code = TREE_CODE (e);
3539 /* We cannot ignore const expressions because it might be a reference
3540 to a const array but whose index contains side-effects. But we can
3541 ignore things that are actual constant or that already have been
3542 handled by this function. */
3544 if (tree_invariant_p (e))
3545 return e;
3547 switch (TREE_CODE_CLASS (code))
3549 case tcc_exceptional:
3550 case tcc_type:
3551 case tcc_declaration:
3552 case tcc_comparison:
3553 case tcc_statement:
3554 case tcc_expression:
3555 case tcc_reference:
3556 case tcc_vl_exp:
3557 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3558 so that it will only be evaluated once. */
3559 /* The reference (r) and comparison (<) classes could be handled as
3560 below, but it is generally faster to only evaluate them once. */
3561 if (TREE_SIDE_EFFECTS (e))
3562 return save_expr (e);
3563 return e;
3565 case tcc_constant:
3566 /* Constants need no processing. In fact, we should never reach
3567 here. */
3568 return e;
3570 case tcc_binary:
3571 /* Division is slow and tends to be compiled with jumps,
3572 especially the division by powers of 2 that is often
3573 found inside of an array reference. So do it just once. */
3574 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3575 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3576 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3577 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3578 return save_expr (e);
3579 /* Recursively stabilize each operand. */
3580 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3581 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3582 break;
3584 case tcc_unary:
3585 /* Recursively stabilize each operand. */
3586 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3587 break;
3589 default:
3590 gcc_unreachable ();
3593 TREE_TYPE (result) = TREE_TYPE (e);
3594 TREE_READONLY (result) = TREE_READONLY (e);
3595 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3596 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3598 return result;
3601 /* Low-level constructors for expressions. */
3603 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3604 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3606 void
3607 recompute_tree_invariant_for_addr_expr (tree t)
3609 tree node;
3610 bool tc = true, se = false;
3612 /* We started out assuming this address is both invariant and constant, but
3613 does not have side effects. Now go down any handled components and see if
3614 any of them involve offsets that are either non-constant or non-invariant.
3615 Also check for side-effects.
3617 ??? Note that this code makes no attempt to deal with the case where
3618 taking the address of something causes a copy due to misalignment. */
3620 #define UPDATE_FLAGS(NODE) \
3621 do { tree _node = (NODE); \
3622 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3623 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3625 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3626 node = TREE_OPERAND (node, 0))
3628 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3629 array reference (probably made temporarily by the G++ front end),
3630 so ignore all the operands. */
3631 if ((TREE_CODE (node) == ARRAY_REF
3632 || TREE_CODE (node) == ARRAY_RANGE_REF)
3633 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3635 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3636 if (TREE_OPERAND (node, 2))
3637 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3638 if (TREE_OPERAND (node, 3))
3639 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3641 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3642 FIELD_DECL, apparently. The G++ front end can put something else
3643 there, at least temporarily. */
3644 else if (TREE_CODE (node) == COMPONENT_REF
3645 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3647 if (TREE_OPERAND (node, 2))
3648 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3650 else if (TREE_CODE (node) == BIT_FIELD_REF)
3651 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3654 node = lang_hooks.expr_to_decl (node, &tc, &se);
3656 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3657 the address, since &(*a)->b is a form of addition. If it's a constant, the
3658 address is constant too. If it's a decl, its address is constant if the
3659 decl is static. Everything else is not constant and, furthermore,
3660 taking the address of a volatile variable is not volatile. */
3661 if (TREE_CODE (node) == INDIRECT_REF
3662 || TREE_CODE (node) == MEM_REF)
3663 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3664 else if (CONSTANT_CLASS_P (node))
3666 else if (DECL_P (node))
3667 tc &= (staticp (node) != NULL_TREE);
3668 else
3670 tc = false;
3671 se |= TREE_SIDE_EFFECTS (node);
3675 TREE_CONSTANT (t) = tc;
3676 TREE_SIDE_EFFECTS (t) = se;
3677 #undef UPDATE_FLAGS
3680 /* Build an expression of code CODE, data type TYPE, and operands as
3681 specified. Expressions and reference nodes can be created this way.
3682 Constants, decls, types and misc nodes cannot be.
3684 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3685 enough for all extant tree codes. */
3687 tree
3688 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3690 tree t;
3692 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3694 t = make_node_stat (code PASS_MEM_STAT);
3695 TREE_TYPE (t) = tt;
3697 return t;
3700 tree
3701 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3703 int length = sizeof (struct tree_exp);
3704 tree t;
3706 record_node_allocation_statistics (code, length);
3708 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3710 t = ggc_alloc_zone_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
3712 memset (t, 0, sizeof (struct tree_common));
3714 TREE_SET_CODE (t, code);
3716 TREE_TYPE (t) = type;
3717 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3718 TREE_OPERAND (t, 0) = node;
3719 TREE_BLOCK (t) = NULL_TREE;
3720 if (node && !TYPE_P (node))
3722 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3723 TREE_READONLY (t) = TREE_READONLY (node);
3726 if (TREE_CODE_CLASS (code) == tcc_statement)
3727 TREE_SIDE_EFFECTS (t) = 1;
3728 else switch (code)
3730 case VA_ARG_EXPR:
3731 /* All of these have side-effects, no matter what their
3732 operands are. */
3733 TREE_SIDE_EFFECTS (t) = 1;
3734 TREE_READONLY (t) = 0;
3735 break;
3737 case INDIRECT_REF:
3738 /* Whether a dereference is readonly has nothing to do with whether
3739 its operand is readonly. */
3740 TREE_READONLY (t) = 0;
3741 break;
3743 case ADDR_EXPR:
3744 if (node)
3745 recompute_tree_invariant_for_addr_expr (t);
3746 break;
3748 default:
3749 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3750 && node && !TYPE_P (node)
3751 && TREE_CONSTANT (node))
3752 TREE_CONSTANT (t) = 1;
3753 if (TREE_CODE_CLASS (code) == tcc_reference
3754 && node && TREE_THIS_VOLATILE (node))
3755 TREE_THIS_VOLATILE (t) = 1;
3756 break;
3759 return t;
3762 #define PROCESS_ARG(N) \
3763 do { \
3764 TREE_OPERAND (t, N) = arg##N; \
3765 if (arg##N &&!TYPE_P (arg##N)) \
3767 if (TREE_SIDE_EFFECTS (arg##N)) \
3768 side_effects = 1; \
3769 if (!TREE_READONLY (arg##N) \
3770 && !CONSTANT_CLASS_P (arg##N)) \
3771 (void) (read_only = 0); \
3772 if (!TREE_CONSTANT (arg##N)) \
3773 (void) (constant = 0); \
3775 } while (0)
3777 tree
3778 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3780 bool constant, read_only, side_effects;
3781 tree t;
3783 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3785 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3786 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3787 /* When sizetype precision doesn't match that of pointers
3788 we need to be able to build explicit extensions or truncations
3789 of the offset argument. */
3790 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3791 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3792 && TREE_CODE (arg1) == INTEGER_CST);
3794 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3795 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3796 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3797 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3799 t = make_node_stat (code PASS_MEM_STAT);
3800 TREE_TYPE (t) = tt;
3802 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3803 result based on those same flags for the arguments. But if the
3804 arguments aren't really even `tree' expressions, we shouldn't be trying
3805 to do this. */
3807 /* Expressions without side effects may be constant if their
3808 arguments are as well. */
3809 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3810 || TREE_CODE_CLASS (code) == tcc_binary);
3811 read_only = 1;
3812 side_effects = TREE_SIDE_EFFECTS (t);
3814 PROCESS_ARG(0);
3815 PROCESS_ARG(1);
3817 TREE_READONLY (t) = read_only;
3818 TREE_CONSTANT (t) = constant;
3819 TREE_SIDE_EFFECTS (t) = side_effects;
3820 TREE_THIS_VOLATILE (t)
3821 = (TREE_CODE_CLASS (code) == tcc_reference
3822 && arg0 && TREE_THIS_VOLATILE (arg0));
3824 return t;
3828 tree
3829 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3830 tree arg2 MEM_STAT_DECL)
3832 bool constant, read_only, side_effects;
3833 tree t;
3835 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3836 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3838 t = make_node_stat (code PASS_MEM_STAT);
3839 TREE_TYPE (t) = tt;
3841 read_only = 1;
3843 /* As a special exception, if COND_EXPR has NULL branches, we
3844 assume that it is a gimple statement and always consider
3845 it to have side effects. */
3846 if (code == COND_EXPR
3847 && tt == void_type_node
3848 && arg1 == NULL_TREE
3849 && arg2 == NULL_TREE)
3850 side_effects = true;
3851 else
3852 side_effects = TREE_SIDE_EFFECTS (t);
3854 PROCESS_ARG(0);
3855 PROCESS_ARG(1);
3856 PROCESS_ARG(2);
3858 if (code == COND_EXPR)
3859 TREE_READONLY (t) = read_only;
3861 TREE_SIDE_EFFECTS (t) = side_effects;
3862 TREE_THIS_VOLATILE (t)
3863 = (TREE_CODE_CLASS (code) == tcc_reference
3864 && arg0 && TREE_THIS_VOLATILE (arg0));
3866 return t;
3869 tree
3870 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3871 tree arg2, tree arg3 MEM_STAT_DECL)
3873 bool constant, read_only, side_effects;
3874 tree t;
3876 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3878 t = make_node_stat (code PASS_MEM_STAT);
3879 TREE_TYPE (t) = tt;
3881 side_effects = TREE_SIDE_EFFECTS (t);
3883 PROCESS_ARG(0);
3884 PROCESS_ARG(1);
3885 PROCESS_ARG(2);
3886 PROCESS_ARG(3);
3888 TREE_SIDE_EFFECTS (t) = side_effects;
3889 TREE_THIS_VOLATILE (t)
3890 = (TREE_CODE_CLASS (code) == tcc_reference
3891 && arg0 && TREE_THIS_VOLATILE (arg0));
3893 return t;
3896 tree
3897 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3898 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3900 bool constant, read_only, side_effects;
3901 tree t;
3903 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3905 t = make_node_stat (code PASS_MEM_STAT);
3906 TREE_TYPE (t) = tt;
3908 side_effects = TREE_SIDE_EFFECTS (t);
3910 PROCESS_ARG(0);
3911 PROCESS_ARG(1);
3912 PROCESS_ARG(2);
3913 PROCESS_ARG(3);
3914 PROCESS_ARG(4);
3916 TREE_SIDE_EFFECTS (t) = side_effects;
3917 TREE_THIS_VOLATILE (t)
3918 = (TREE_CODE_CLASS (code) == tcc_reference
3919 && arg0 && TREE_THIS_VOLATILE (arg0));
3921 return t;
3924 tree
3925 build6_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3926 tree arg2, tree arg3, tree arg4, tree arg5 MEM_STAT_DECL)
3928 bool constant, read_only, side_effects;
3929 tree t;
3931 gcc_assert (code == TARGET_MEM_REF);
3933 t = make_node_stat (code PASS_MEM_STAT);
3934 TREE_TYPE (t) = tt;
3936 side_effects = TREE_SIDE_EFFECTS (t);
3938 PROCESS_ARG(0);
3939 PROCESS_ARG(1);
3940 PROCESS_ARG(2);
3941 PROCESS_ARG(3);
3942 PROCESS_ARG(4);
3943 if (code == TARGET_MEM_REF)
3944 side_effects = 0;
3945 PROCESS_ARG(5);
3947 TREE_SIDE_EFFECTS (t) = side_effects;
3948 TREE_THIS_VOLATILE (t)
3949 = (code == TARGET_MEM_REF
3950 && arg5 && TREE_THIS_VOLATILE (arg5));
3952 return t;
3955 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
3956 on the pointer PTR. */
3958 tree
3959 build_simple_mem_ref_loc (location_t loc, tree ptr)
3961 HOST_WIDE_INT offset = 0;
3962 tree ptype = TREE_TYPE (ptr);
3963 tree tem;
3964 /* For convenience allow addresses that collapse to a simple base
3965 and offset. */
3966 if (TREE_CODE (ptr) == ADDR_EXPR
3967 && (handled_component_p (TREE_OPERAND (ptr, 0))
3968 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
3970 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
3971 gcc_assert (ptr);
3972 ptr = build_fold_addr_expr (ptr);
3973 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
3975 tem = build2 (MEM_REF, TREE_TYPE (ptype),
3976 ptr, build_int_cst (ptype, offset));
3977 SET_EXPR_LOCATION (tem, loc);
3978 return tem;
3981 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
3983 double_int
3984 mem_ref_offset (const_tree t)
3986 tree toff = TREE_OPERAND (t, 1);
3987 return double_int_sext (tree_to_double_int (toff),
3988 TYPE_PRECISION (TREE_TYPE (toff)));
3991 /* Return the pointer-type relevant for TBAA purposes from the
3992 gimple memory reference tree T. This is the type to be used for
3993 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
3995 tree
3996 reference_alias_ptr_type (const_tree t)
3998 const_tree base = t;
3999 while (handled_component_p (base))
4000 base = TREE_OPERAND (base, 0);
4001 if (TREE_CODE (base) == MEM_REF)
4002 return TREE_TYPE (TREE_OPERAND (base, 1));
4003 else if (TREE_CODE (base) == TARGET_MEM_REF)
4004 return TREE_TYPE (TMR_OFFSET (base));
4005 else
4006 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4009 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4010 offsetted by OFFSET units. */
4012 tree
4013 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4015 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4016 build_fold_addr_expr (base),
4017 build_int_cst (ptr_type_node, offset));
4018 tree addr = build1 (ADDR_EXPR, type, ref);
4019 recompute_tree_invariant_for_addr_expr (addr);
4020 return addr;
4023 /* Similar except don't specify the TREE_TYPE
4024 and leave the TREE_SIDE_EFFECTS as 0.
4025 It is permissible for arguments to be null,
4026 or even garbage if their values do not matter. */
4028 tree
4029 build_nt (enum tree_code code, ...)
4031 tree t;
4032 int length;
4033 int i;
4034 va_list p;
4036 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4038 va_start (p, code);
4040 t = make_node (code);
4041 length = TREE_CODE_LENGTH (code);
4043 for (i = 0; i < length; i++)
4044 TREE_OPERAND (t, i) = va_arg (p, tree);
4046 va_end (p);
4047 return t;
4050 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4051 tree VEC. */
4053 tree
4054 build_nt_call_vec (tree fn, VEC(tree,gc) *args)
4056 tree ret, t;
4057 unsigned int ix;
4059 ret = build_vl_exp (CALL_EXPR, VEC_length (tree, args) + 3);
4060 CALL_EXPR_FN (ret) = fn;
4061 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4062 FOR_EACH_VEC_ELT (tree, args, ix, t)
4063 CALL_EXPR_ARG (ret, ix) = t;
4064 return ret;
4067 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4068 We do NOT enter this node in any sort of symbol table.
4070 LOC is the location of the decl.
4072 layout_decl is used to set up the decl's storage layout.
4073 Other slots are initialized to 0 or null pointers. */
4075 tree
4076 build_decl_stat (location_t loc, enum tree_code code, tree name,
4077 tree type MEM_STAT_DECL)
4079 tree t;
4081 t = make_node_stat (code PASS_MEM_STAT);
4082 DECL_SOURCE_LOCATION (t) = loc;
4084 /* if (type == error_mark_node)
4085 type = integer_type_node; */
4086 /* That is not done, deliberately, so that having error_mark_node
4087 as the type can suppress useless errors in the use of this variable. */
4089 DECL_NAME (t) = name;
4090 TREE_TYPE (t) = type;
4092 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4093 layout_decl (t, 0);
4095 return t;
4098 /* Builds and returns function declaration with NAME and TYPE. */
4100 tree
4101 build_fn_decl (const char *name, tree type)
4103 tree id = get_identifier (name);
4104 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4106 DECL_EXTERNAL (decl) = 1;
4107 TREE_PUBLIC (decl) = 1;
4108 DECL_ARTIFICIAL (decl) = 1;
4109 TREE_NOTHROW (decl) = 1;
4111 return decl;
4114 VEC(tree,gc) *all_translation_units;
4116 /* Builds a new translation-unit decl with name NAME, queues it in the
4117 global list of translation-unit decls and returns it. */
4119 tree
4120 build_translation_unit_decl (tree name)
4122 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4123 name, NULL_TREE);
4124 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4125 VEC_safe_push (tree, gc, all_translation_units, tu);
4126 return tu;
4130 /* BLOCK nodes are used to represent the structure of binding contours
4131 and declarations, once those contours have been exited and their contents
4132 compiled. This information is used for outputting debugging info. */
4134 tree
4135 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4137 tree block = make_node (BLOCK);
4139 BLOCK_VARS (block) = vars;
4140 BLOCK_SUBBLOCKS (block) = subblocks;
4141 BLOCK_SUPERCONTEXT (block) = supercontext;
4142 BLOCK_CHAIN (block) = chain;
4143 return block;
4147 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4149 LOC is the location to use in tree T. */
4151 void
4152 protected_set_expr_location (tree t, location_t loc)
4154 if (t && CAN_HAVE_LOCATION_P (t))
4155 SET_EXPR_LOCATION (t, loc);
4158 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4159 is ATTRIBUTE. */
4161 tree
4162 build_decl_attribute_variant (tree ddecl, tree attribute)
4164 DECL_ATTRIBUTES (ddecl) = attribute;
4165 return ddecl;
4168 /* Borrowed from hashtab.c iterative_hash implementation. */
4169 #define mix(a,b,c) \
4171 a -= b; a -= c; a ^= (c>>13); \
4172 b -= c; b -= a; b ^= (a<< 8); \
4173 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4174 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4175 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4176 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4177 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4178 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4179 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4183 /* Produce good hash value combining VAL and VAL2. */
4184 hashval_t
4185 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4187 /* the golden ratio; an arbitrary value. */
4188 hashval_t a = 0x9e3779b9;
4190 mix (a, val, val2);
4191 return val2;
4194 /* Produce good hash value combining VAL and VAL2. */
4195 hashval_t
4196 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4198 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4199 return iterative_hash_hashval_t (val, val2);
4200 else
4202 hashval_t a = (hashval_t) val;
4203 /* Avoid warnings about shifting of more than the width of the type on
4204 hosts that won't execute this path. */
4205 int zero = 0;
4206 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4207 mix (a, b, val2);
4208 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4210 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4211 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4212 mix (a, b, val2);
4214 return val2;
4218 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4219 is ATTRIBUTE and its qualifiers are QUALS.
4221 Record such modified types already made so we don't make duplicates. */
4223 tree
4224 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4226 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4228 hashval_t hashcode = 0;
4229 tree ntype;
4230 enum tree_code code = TREE_CODE (ttype);
4232 /* Building a distinct copy of a tagged type is inappropriate; it
4233 causes breakage in code that expects there to be a one-to-one
4234 relationship between a struct and its fields.
4235 build_duplicate_type is another solution (as used in
4236 handle_transparent_union_attribute), but that doesn't play well
4237 with the stronger C++ type identity model. */
4238 if (TREE_CODE (ttype) == RECORD_TYPE
4239 || TREE_CODE (ttype) == UNION_TYPE
4240 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4241 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4243 warning (OPT_Wattributes,
4244 "ignoring attributes applied to %qT after definition",
4245 TYPE_MAIN_VARIANT (ttype));
4246 return build_qualified_type (ttype, quals);
4249 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4250 ntype = build_distinct_type_copy (ttype);
4252 TYPE_ATTRIBUTES (ntype) = attribute;
4254 hashcode = iterative_hash_object (code, hashcode);
4255 if (TREE_TYPE (ntype))
4256 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4257 hashcode);
4258 hashcode = attribute_hash_list (attribute, hashcode);
4260 switch (TREE_CODE (ntype))
4262 case FUNCTION_TYPE:
4263 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4264 break;
4265 case ARRAY_TYPE:
4266 if (TYPE_DOMAIN (ntype))
4267 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4268 hashcode);
4269 break;
4270 case INTEGER_TYPE:
4271 hashcode = iterative_hash_object
4272 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4273 hashcode = iterative_hash_object
4274 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4275 break;
4276 case REAL_TYPE:
4277 case FIXED_POINT_TYPE:
4279 unsigned int precision = TYPE_PRECISION (ntype);
4280 hashcode = iterative_hash_object (precision, hashcode);
4282 break;
4283 default:
4284 break;
4287 ntype = type_hash_canon (hashcode, ntype);
4289 /* If the target-dependent attributes make NTYPE different from
4290 its canonical type, we will need to use structural equality
4291 checks for this type. */
4292 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4293 || !comp_type_attributes (ntype, ttype))
4294 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4295 else if (TYPE_CANONICAL (ntype) == ntype)
4296 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4298 ttype = build_qualified_type (ntype, quals);
4300 else if (TYPE_QUALS (ttype) != quals)
4301 ttype = build_qualified_type (ttype, quals);
4303 return ttype;
4306 /* Compare two attributes for their value identity. Return true if the
4307 attribute values are known to be equal; otherwise return false.
4310 static bool
4311 attribute_value_equal (const_tree attr1, const_tree attr2)
4313 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4314 return true;
4316 if (TREE_VALUE (attr1) != NULL_TREE
4317 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4318 && TREE_VALUE (attr2) != NULL
4319 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4320 return (simple_cst_list_equal (TREE_VALUE (attr1),
4321 TREE_VALUE (attr2)) == 1);
4323 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4326 /* Return 0 if the attributes for two types are incompatible, 1 if they
4327 are compatible, and 2 if they are nearly compatible (which causes a
4328 warning to be generated). */
4330 comp_type_attributes (const_tree type1, const_tree type2)
4332 const_tree a1 = TYPE_ATTRIBUTES (type1);
4333 const_tree a2 = TYPE_ATTRIBUTES (type2);
4334 const_tree a;
4336 if (a1 == a2)
4337 return 1;
4338 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4340 const struct attribute_spec *as;
4341 const_tree attr;
4343 as = lookup_attribute_spec (TREE_PURPOSE (a));
4344 if (!as || as->affects_type_identity == false)
4345 continue;
4347 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4348 if (!attr || !attribute_value_equal (a, attr))
4349 break;
4351 if (!a)
4353 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4355 const struct attribute_spec *as;
4357 as = lookup_attribute_spec (TREE_PURPOSE (a));
4358 if (!as || as->affects_type_identity == false)
4359 continue;
4361 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4362 break;
4363 /* We don't need to compare trees again, as we did this
4364 already in first loop. */
4366 /* All types - affecting identity - are equal, so
4367 there is no need to call target hook for comparison. */
4368 if (!a)
4369 return 1;
4371 /* As some type combinations - like default calling-convention - might
4372 be compatible, we have to call the target hook to get the final result. */
4373 return targetm.comp_type_attributes (type1, type2);
4376 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4377 is ATTRIBUTE.
4379 Record such modified types already made so we don't make duplicates. */
4381 tree
4382 build_type_attribute_variant (tree ttype, tree attribute)
4384 return build_type_attribute_qual_variant (ttype, attribute,
4385 TYPE_QUALS (ttype));
4389 /* Reset the expression *EXPR_P, a size or position.
4391 ??? We could reset all non-constant sizes or positions. But it's cheap
4392 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4394 We need to reset self-referential sizes or positions because they cannot
4395 be gimplified and thus can contain a CALL_EXPR after the gimplification
4396 is finished, which will run afoul of LTO streaming. And they need to be
4397 reset to something essentially dummy but not constant, so as to preserve
4398 the properties of the object they are attached to. */
4400 static inline void
4401 free_lang_data_in_one_sizepos (tree *expr_p)
4403 tree expr = *expr_p;
4404 if (CONTAINS_PLACEHOLDER_P (expr))
4405 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4409 /* Reset all the fields in a binfo node BINFO. We only keep
4410 BINFO_VIRTUALS, which is used by gimple_fold_obj_type_ref. */
4412 static void
4413 free_lang_data_in_binfo (tree binfo)
4415 unsigned i;
4416 tree t;
4418 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4420 BINFO_VTABLE (binfo) = NULL_TREE;
4421 BINFO_BASE_ACCESSES (binfo) = NULL;
4422 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4423 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4425 FOR_EACH_VEC_ELT (tree, BINFO_BASE_BINFOS (binfo), i, t)
4426 free_lang_data_in_binfo (t);
4430 /* Reset all language specific information still present in TYPE. */
4432 static void
4433 free_lang_data_in_type (tree type)
4435 gcc_assert (TYPE_P (type));
4437 /* Give the FE a chance to remove its own data first. */
4438 lang_hooks.free_lang_data (type);
4440 TREE_LANG_FLAG_0 (type) = 0;
4441 TREE_LANG_FLAG_1 (type) = 0;
4442 TREE_LANG_FLAG_2 (type) = 0;
4443 TREE_LANG_FLAG_3 (type) = 0;
4444 TREE_LANG_FLAG_4 (type) = 0;
4445 TREE_LANG_FLAG_5 (type) = 0;
4446 TREE_LANG_FLAG_6 (type) = 0;
4448 if (TREE_CODE (type) == FUNCTION_TYPE)
4450 /* Remove the const and volatile qualifiers from arguments. The
4451 C++ front end removes them, but the C front end does not,
4452 leading to false ODR violation errors when merging two
4453 instances of the same function signature compiled by
4454 different front ends. */
4455 tree p;
4457 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4459 tree arg_type = TREE_VALUE (p);
4461 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4463 int quals = TYPE_QUALS (arg_type)
4464 & ~TYPE_QUAL_CONST
4465 & ~TYPE_QUAL_VOLATILE;
4466 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4467 free_lang_data_in_type (TREE_VALUE (p));
4472 /* Remove members that are not actually FIELD_DECLs from the field
4473 list of an aggregate. These occur in C++. */
4474 if (RECORD_OR_UNION_TYPE_P (type))
4476 tree prev, member;
4478 /* Note that TYPE_FIELDS can be shared across distinct
4479 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4480 to be removed, we cannot set its TREE_CHAIN to NULL.
4481 Otherwise, we would not be able to find all the other fields
4482 in the other instances of this TREE_TYPE.
4484 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4485 prev = NULL_TREE;
4486 member = TYPE_FIELDS (type);
4487 while (member)
4489 if (TREE_CODE (member) == FIELD_DECL)
4491 if (prev)
4492 TREE_CHAIN (prev) = member;
4493 else
4494 TYPE_FIELDS (type) = member;
4495 prev = member;
4498 member = TREE_CHAIN (member);
4501 if (prev)
4502 TREE_CHAIN (prev) = NULL_TREE;
4503 else
4504 TYPE_FIELDS (type) = NULL_TREE;
4506 TYPE_METHODS (type) = NULL_TREE;
4507 if (TYPE_BINFO (type))
4508 free_lang_data_in_binfo (TYPE_BINFO (type));
4510 else
4512 /* For non-aggregate types, clear out the language slot (which
4513 overloads TYPE_BINFO). */
4514 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4516 if (INTEGRAL_TYPE_P (type)
4517 || SCALAR_FLOAT_TYPE_P (type)
4518 || FIXED_POINT_TYPE_P (type))
4520 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4521 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4525 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4526 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4528 if (debug_info_level < DINFO_LEVEL_TERSE
4529 || (TYPE_CONTEXT (type)
4530 && TREE_CODE (TYPE_CONTEXT (type)) != FUNCTION_DECL
4531 && TREE_CODE (TYPE_CONTEXT (type)) != NAMESPACE_DECL))
4532 TYPE_CONTEXT (type) = NULL_TREE;
4534 if (debug_info_level < DINFO_LEVEL_TERSE)
4535 TYPE_STUB_DECL (type) = NULL_TREE;
4539 /* Return true if DECL may need an assembler name to be set. */
4541 static inline bool
4542 need_assembler_name_p (tree decl)
4544 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4545 if (TREE_CODE (decl) != FUNCTION_DECL
4546 && TREE_CODE (decl) != VAR_DECL)
4547 return false;
4549 /* If DECL already has its assembler name set, it does not need a
4550 new one. */
4551 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4552 || DECL_ASSEMBLER_NAME_SET_P (decl))
4553 return false;
4555 /* Abstract decls do not need an assembler name. */
4556 if (DECL_ABSTRACT (decl))
4557 return false;
4559 /* For VAR_DECLs, only static, public and external symbols need an
4560 assembler name. */
4561 if (TREE_CODE (decl) == VAR_DECL
4562 && !TREE_STATIC (decl)
4563 && !TREE_PUBLIC (decl)
4564 && !DECL_EXTERNAL (decl))
4565 return false;
4567 if (TREE_CODE (decl) == FUNCTION_DECL)
4569 /* Do not set assembler name on builtins. Allow RTL expansion to
4570 decide whether to expand inline or via a regular call. */
4571 if (DECL_BUILT_IN (decl)
4572 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4573 return false;
4575 /* Functions represented in the callgraph need an assembler name. */
4576 if (cgraph_get_node (decl) != NULL)
4577 return true;
4579 /* Unused and not public functions don't need an assembler name. */
4580 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4581 return false;
4584 return true;
4588 /* Reset all language specific information still present in symbol
4589 DECL. */
4591 static void
4592 free_lang_data_in_decl (tree decl)
4594 gcc_assert (DECL_P (decl));
4596 /* Give the FE a chance to remove its own data first. */
4597 lang_hooks.free_lang_data (decl);
4599 TREE_LANG_FLAG_0 (decl) = 0;
4600 TREE_LANG_FLAG_1 (decl) = 0;
4601 TREE_LANG_FLAG_2 (decl) = 0;
4602 TREE_LANG_FLAG_3 (decl) = 0;
4603 TREE_LANG_FLAG_4 (decl) = 0;
4604 TREE_LANG_FLAG_5 (decl) = 0;
4605 TREE_LANG_FLAG_6 (decl) = 0;
4607 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4608 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4609 if (TREE_CODE (decl) == FIELD_DECL)
4610 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4612 /* DECL_FCONTEXT is only used for debug info generation. */
4613 if (TREE_CODE (decl) == FIELD_DECL
4614 && debug_info_level < DINFO_LEVEL_TERSE)
4615 DECL_FCONTEXT (decl) = NULL_TREE;
4617 if (TREE_CODE (decl) == FUNCTION_DECL)
4619 if (gimple_has_body_p (decl))
4621 tree t;
4623 /* If DECL has a gimple body, then the context for its
4624 arguments must be DECL. Otherwise, it doesn't really
4625 matter, as we will not be emitting any code for DECL. In
4626 general, there may be other instances of DECL created by
4627 the front end and since PARM_DECLs are generally shared,
4628 their DECL_CONTEXT changes as the replicas of DECL are
4629 created. The only time where DECL_CONTEXT is important
4630 is for the FUNCTION_DECLs that have a gimple body (since
4631 the PARM_DECL will be used in the function's body). */
4632 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4633 DECL_CONTEXT (t) = decl;
4636 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4637 At this point, it is not needed anymore. */
4638 DECL_SAVED_TREE (decl) = NULL_TREE;
4640 /* Clear the abstract origin if it refers to a method. Otherwise
4641 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4642 origin will not be output correctly. */
4643 if (DECL_ABSTRACT_ORIGIN (decl)
4644 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4645 && RECORD_OR_UNION_TYPE_P
4646 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4647 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4649 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4650 DECL_VINDEX referring to itself into a vtable slot number as it
4651 should. Happens with functions that are copied and then forgotten
4652 about. Just clear it, it won't matter anymore. */
4653 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4654 DECL_VINDEX (decl) = NULL_TREE;
4656 else if (TREE_CODE (decl) == VAR_DECL)
4658 if ((DECL_EXTERNAL (decl)
4659 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4660 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4661 DECL_INITIAL (decl) = NULL_TREE;
4663 else if (TREE_CODE (decl) == TYPE_DECL)
4664 DECL_INITIAL (decl) = NULL_TREE;
4665 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4666 && DECL_INITIAL (decl)
4667 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4669 /* Strip builtins from the translation-unit BLOCK. We still have
4670 targets without builtin_decl support and also builtins are
4671 shared nodes and thus we can't use TREE_CHAIN in multiple
4672 lists. */
4673 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4674 while (*nextp)
4676 tree var = *nextp;
4677 if (TREE_CODE (var) == FUNCTION_DECL
4678 && DECL_BUILT_IN (var))
4679 *nextp = TREE_CHAIN (var);
4680 else
4681 nextp = &TREE_CHAIN (var);
4687 /* Data used when collecting DECLs and TYPEs for language data removal. */
4689 struct free_lang_data_d
4691 /* Worklist to avoid excessive recursion. */
4692 VEC(tree,heap) *worklist;
4694 /* Set of traversed objects. Used to avoid duplicate visits. */
4695 struct pointer_set_t *pset;
4697 /* Array of symbols to process with free_lang_data_in_decl. */
4698 VEC(tree,heap) *decls;
4700 /* Array of types to process with free_lang_data_in_type. */
4701 VEC(tree,heap) *types;
4705 /* Save all language fields needed to generate proper debug information
4706 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4708 static void
4709 save_debug_info_for_decl (tree t)
4711 /*struct saved_debug_info_d *sdi;*/
4713 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4715 /* FIXME. Partial implementation for saving debug info removed. */
4719 /* Save all language fields needed to generate proper debug information
4720 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4722 static void
4723 save_debug_info_for_type (tree t)
4725 /*struct saved_debug_info_d *sdi;*/
4727 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4729 /* FIXME. Partial implementation for saving debug info removed. */
4733 /* Add type or decl T to one of the list of tree nodes that need their
4734 language data removed. The lists are held inside FLD. */
4736 static void
4737 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4739 if (DECL_P (t))
4741 VEC_safe_push (tree, heap, fld->decls, t);
4742 if (debug_info_level > DINFO_LEVEL_TERSE)
4743 save_debug_info_for_decl (t);
4745 else if (TYPE_P (t))
4747 VEC_safe_push (tree, heap, fld->types, t);
4748 if (debug_info_level > DINFO_LEVEL_TERSE)
4749 save_debug_info_for_type (t);
4751 else
4752 gcc_unreachable ();
4755 /* Push tree node T into FLD->WORKLIST. */
4757 static inline void
4758 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4760 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4761 VEC_safe_push (tree, heap, fld->worklist, (t));
4765 /* Operand callback helper for free_lang_data_in_node. *TP is the
4766 subtree operand being considered. */
4768 static tree
4769 find_decls_types_r (tree *tp, int *ws, void *data)
4771 tree t = *tp;
4772 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4774 if (TREE_CODE (t) == TREE_LIST)
4775 return NULL_TREE;
4777 /* Language specific nodes will be removed, so there is no need
4778 to gather anything under them. */
4779 if (is_lang_specific (t))
4781 *ws = 0;
4782 return NULL_TREE;
4785 if (DECL_P (t))
4787 /* Note that walk_tree does not traverse every possible field in
4788 decls, so we have to do our own traversals here. */
4789 add_tree_to_fld_list (t, fld);
4791 fld_worklist_push (DECL_NAME (t), fld);
4792 fld_worklist_push (DECL_CONTEXT (t), fld);
4793 fld_worklist_push (DECL_SIZE (t), fld);
4794 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4796 /* We are going to remove everything under DECL_INITIAL for
4797 TYPE_DECLs. No point walking them. */
4798 if (TREE_CODE (t) != TYPE_DECL)
4799 fld_worklist_push (DECL_INITIAL (t), fld);
4801 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4802 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4804 if (TREE_CODE (t) == FUNCTION_DECL)
4806 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4807 fld_worklist_push (DECL_RESULT (t), fld);
4809 else if (TREE_CODE (t) == TYPE_DECL)
4811 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4812 fld_worklist_push (DECL_VINDEX (t), fld);
4814 else if (TREE_CODE (t) == FIELD_DECL)
4816 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4817 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4818 fld_worklist_push (DECL_QUALIFIER (t), fld);
4819 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4820 fld_worklist_push (DECL_FCONTEXT (t), fld);
4822 else if (TREE_CODE (t) == VAR_DECL)
4824 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4825 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4828 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4829 && DECL_HAS_VALUE_EXPR_P (t))
4830 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4832 if (TREE_CODE (t) != FIELD_DECL
4833 && TREE_CODE (t) != TYPE_DECL)
4834 fld_worklist_push (TREE_CHAIN (t), fld);
4835 *ws = 0;
4837 else if (TYPE_P (t))
4839 /* Note that walk_tree does not traverse every possible field in
4840 types, so we have to do our own traversals here. */
4841 add_tree_to_fld_list (t, fld);
4843 if (!RECORD_OR_UNION_TYPE_P (t))
4844 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4845 fld_worklist_push (TYPE_SIZE (t), fld);
4846 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4847 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4848 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4849 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4850 fld_worklist_push (TYPE_NAME (t), fld);
4851 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4852 them and thus do not and want not to reach unused pointer types
4853 this way. */
4854 if (!POINTER_TYPE_P (t))
4855 fld_worklist_push (TYPE_MINVAL (t), fld);
4856 if (!RECORD_OR_UNION_TYPE_P (t))
4857 fld_worklist_push (TYPE_MAXVAL (t), fld);
4858 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4859 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4860 do not and want not to reach unused variants this way. */
4861 fld_worklist_push (TYPE_CONTEXT (t), fld);
4862 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4863 and want not to reach unused types this way. */
4865 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4867 unsigned i;
4868 tree tem;
4869 for (i = 0; VEC_iterate (tree, BINFO_BASE_BINFOS (TYPE_BINFO (t)),
4870 i, tem); ++i)
4871 fld_worklist_push (TREE_TYPE (tem), fld);
4872 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4873 if (tem
4874 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4875 && TREE_CODE (tem) == TREE_LIST)
4878 fld_worklist_push (TREE_VALUE (tem), fld);
4879 tem = TREE_CHAIN (tem);
4881 while (tem);
4883 if (RECORD_OR_UNION_TYPE_P (t))
4885 tree tem;
4886 /* Push all TYPE_FIELDS - there can be interleaving interesting
4887 and non-interesting things. */
4888 tem = TYPE_FIELDS (t);
4889 while (tem)
4891 if (TREE_CODE (tem) == FIELD_DECL)
4892 fld_worklist_push (tem, fld);
4893 tem = TREE_CHAIN (tem);
4897 fld_worklist_push (TREE_CHAIN (t), fld);
4898 *ws = 0;
4900 else if (TREE_CODE (t) == BLOCK)
4902 tree tem;
4903 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
4904 fld_worklist_push (tem, fld);
4905 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
4906 fld_worklist_push (tem, fld);
4907 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
4910 if (TREE_CODE (t) != IDENTIFIER_NODE
4911 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
4912 fld_worklist_push (TREE_TYPE (t), fld);
4914 return NULL_TREE;
4918 /* Find decls and types in T. */
4920 static void
4921 find_decls_types (tree t, struct free_lang_data_d *fld)
4923 while (1)
4925 if (!pointer_set_contains (fld->pset, t))
4926 walk_tree (&t, find_decls_types_r, fld, fld->pset);
4927 if (VEC_empty (tree, fld->worklist))
4928 break;
4929 t = VEC_pop (tree, fld->worklist);
4933 /* Translate all the types in LIST with the corresponding runtime
4934 types. */
4936 static tree
4937 get_eh_types_for_runtime (tree list)
4939 tree head, prev;
4941 if (list == NULL_TREE)
4942 return NULL_TREE;
4944 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
4945 prev = head;
4946 list = TREE_CHAIN (list);
4947 while (list)
4949 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
4950 TREE_CHAIN (prev) = n;
4951 prev = TREE_CHAIN (prev);
4952 list = TREE_CHAIN (list);
4955 return head;
4959 /* Find decls and types referenced in EH region R and store them in
4960 FLD->DECLS and FLD->TYPES. */
4962 static void
4963 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
4965 switch (r->type)
4967 case ERT_CLEANUP:
4968 break;
4970 case ERT_TRY:
4972 eh_catch c;
4974 /* The types referenced in each catch must first be changed to the
4975 EH types used at runtime. This removes references to FE types
4976 in the region. */
4977 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
4979 c->type_list = get_eh_types_for_runtime (c->type_list);
4980 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
4983 break;
4985 case ERT_ALLOWED_EXCEPTIONS:
4986 r->u.allowed.type_list
4987 = get_eh_types_for_runtime (r->u.allowed.type_list);
4988 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
4989 break;
4991 case ERT_MUST_NOT_THROW:
4992 walk_tree (&r->u.must_not_throw.failure_decl,
4993 find_decls_types_r, fld, fld->pset);
4994 break;
4999 /* Find decls and types referenced in cgraph node N and store them in
5000 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5001 look for *every* kind of DECL and TYPE node reachable from N,
5002 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5003 NAMESPACE_DECLs, etc). */
5005 static void
5006 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5008 basic_block bb;
5009 struct function *fn;
5010 unsigned ix;
5011 tree t;
5013 find_decls_types (n->decl, fld);
5015 if (!gimple_has_body_p (n->decl))
5016 return;
5018 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5020 fn = DECL_STRUCT_FUNCTION (n->decl);
5022 /* Traverse locals. */
5023 FOR_EACH_LOCAL_DECL (fn, ix, t)
5024 find_decls_types (t, fld);
5026 /* Traverse EH regions in FN. */
5028 eh_region r;
5029 FOR_ALL_EH_REGION_FN (r, fn)
5030 find_decls_types_in_eh_region (r, fld);
5033 /* Traverse every statement in FN. */
5034 FOR_EACH_BB_FN (bb, fn)
5036 gimple_stmt_iterator si;
5037 unsigned i;
5039 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5041 gimple phi = gsi_stmt (si);
5043 for (i = 0; i < gimple_phi_num_args (phi); i++)
5045 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5046 find_decls_types (*arg_p, fld);
5050 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5052 gimple stmt = gsi_stmt (si);
5054 for (i = 0; i < gimple_num_ops (stmt); i++)
5056 tree arg = gimple_op (stmt, i);
5057 find_decls_types (arg, fld);
5064 /* Find decls and types referenced in varpool node N and store them in
5065 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5066 look for *every* kind of DECL and TYPE node reachable from N,
5067 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5068 NAMESPACE_DECLs, etc). */
5070 static void
5071 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5073 find_decls_types (v->decl, fld);
5076 /* If T needs an assembler name, have one created for it. */
5078 void
5079 assign_assembler_name_if_neeeded (tree t)
5081 if (need_assembler_name_p (t))
5083 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5084 diagnostics that use input_location to show locus
5085 information. The problem here is that, at this point,
5086 input_location is generally anchored to the end of the file
5087 (since the parser is long gone), so we don't have a good
5088 position to pin it to.
5090 To alleviate this problem, this uses the location of T's
5091 declaration. Examples of this are
5092 testsuite/g++.dg/template/cond2.C and
5093 testsuite/g++.dg/template/pr35240.C. */
5094 location_t saved_location = input_location;
5095 input_location = DECL_SOURCE_LOCATION (t);
5097 decl_assembler_name (t);
5099 input_location = saved_location;
5104 /* Free language specific information for every operand and expression
5105 in every node of the call graph. This process operates in three stages:
5107 1- Every callgraph node and varpool node is traversed looking for
5108 decls and types embedded in them. This is a more exhaustive
5109 search than that done by find_referenced_vars, because it will
5110 also collect individual fields, decls embedded in types, etc.
5112 2- All the decls found are sent to free_lang_data_in_decl.
5114 3- All the types found are sent to free_lang_data_in_type.
5116 The ordering between decls and types is important because
5117 free_lang_data_in_decl sets assembler names, which includes
5118 mangling. So types cannot be freed up until assembler names have
5119 been set up. */
5121 static void
5122 free_lang_data_in_cgraph (void)
5124 struct cgraph_node *n;
5125 struct varpool_node *v;
5126 struct free_lang_data_d fld;
5127 tree t;
5128 unsigned i;
5129 alias_pair *p;
5131 /* Initialize sets and arrays to store referenced decls and types. */
5132 fld.pset = pointer_set_create ();
5133 fld.worklist = NULL;
5134 fld.decls = VEC_alloc (tree, heap, 100);
5135 fld.types = VEC_alloc (tree, heap, 100);
5137 /* Find decls and types in the body of every function in the callgraph. */
5138 for (n = cgraph_nodes; n; n = n->next)
5139 find_decls_types_in_node (n, &fld);
5141 FOR_EACH_VEC_ELT (alias_pair, alias_pairs, i, p)
5142 find_decls_types (p->decl, &fld);
5144 /* Find decls and types in every varpool symbol. */
5145 for (v = varpool_nodes; v; v = v->next)
5146 find_decls_types_in_var (v, &fld);
5148 /* Set the assembler name on every decl found. We need to do this
5149 now because free_lang_data_in_decl will invalidate data needed
5150 for mangling. This breaks mangling on interdependent decls. */
5151 FOR_EACH_VEC_ELT (tree, fld.decls, i, t)
5152 assign_assembler_name_if_neeeded (t);
5154 /* Traverse every decl found freeing its language data. */
5155 FOR_EACH_VEC_ELT (tree, fld.decls, i, t)
5156 free_lang_data_in_decl (t);
5158 /* Traverse every type found freeing its language data. */
5159 FOR_EACH_VEC_ELT (tree, fld.types, i, t)
5160 free_lang_data_in_type (t);
5162 pointer_set_destroy (fld.pset);
5163 VEC_free (tree, heap, fld.worklist);
5164 VEC_free (tree, heap, fld.decls);
5165 VEC_free (tree, heap, fld.types);
5169 /* Free resources that are used by FE but are not needed once they are done. */
5171 static unsigned
5172 free_lang_data (void)
5174 unsigned i;
5176 /* If we are the LTO frontend we have freed lang-specific data already. */
5177 if (in_lto_p
5178 || !flag_generate_lto)
5179 return 0;
5181 /* Allocate and assign alias sets to the standard integer types
5182 while the slots are still in the way the frontends generated them. */
5183 for (i = 0; i < itk_none; ++i)
5184 if (integer_types[i])
5185 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5187 /* Traverse the IL resetting language specific information for
5188 operands, expressions, etc. */
5189 free_lang_data_in_cgraph ();
5191 /* Create gimple variants for common types. */
5192 ptrdiff_type_node = integer_type_node;
5193 fileptr_type_node = ptr_type_node;
5194 if (TREE_CODE (boolean_type_node) != BOOLEAN_TYPE
5195 || (TYPE_MODE (boolean_type_node)
5196 != mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0))
5197 || TYPE_PRECISION (boolean_type_node) != 1
5198 || !TYPE_UNSIGNED (boolean_type_node))
5200 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
5201 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
5202 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
5203 TYPE_PRECISION (boolean_type_node) = 1;
5204 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
5205 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
5208 /* Unify char_type_node with its properly signed variant. */
5209 if (TYPE_UNSIGNED (char_type_node))
5210 unsigned_char_type_node = char_type_node;
5211 else
5212 signed_char_type_node = char_type_node;
5214 /* Reset some langhooks. Do not reset types_compatible_p, it may
5215 still be used indirectly via the get_alias_set langhook. */
5216 lang_hooks.callgraph.analyze_expr = NULL;
5217 lang_hooks.dwarf_name = lhd_dwarf_name;
5218 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5219 /* We do not want the default decl_assembler_name implementation,
5220 rather if we have fixed everything we want a wrapper around it
5221 asserting that all non-local symbols already got their assembler
5222 name and only produce assembler names for local symbols. Or rather
5223 make sure we never call decl_assembler_name on local symbols and
5224 devise a separate, middle-end private scheme for it. */
5226 /* Reset diagnostic machinery. */
5227 diagnostic_starter (global_dc) = default_tree_diagnostic_starter;
5228 diagnostic_finalizer (global_dc) = default_diagnostic_finalizer;
5229 diagnostic_format_decoder (global_dc) = default_tree_printer;
5231 return 0;
5235 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5238 SIMPLE_IPA_PASS,
5239 "*free_lang_data", /* name */
5240 NULL, /* gate */
5241 free_lang_data, /* execute */
5242 NULL, /* sub */
5243 NULL, /* next */
5244 0, /* static_pass_number */
5245 TV_IPA_FREE_LANG_DATA, /* tv_id */
5246 0, /* properties_required */
5247 0, /* properties_provided */
5248 0, /* properties_destroyed */
5249 0, /* todo_flags_start */
5250 TODO_ggc_collect /* todo_flags_finish */
5254 /* Return nonzero if IDENT is a valid name for attribute ATTR,
5255 or zero if not.
5257 We try both `text' and `__text__', ATTR may be either one. */
5258 /* ??? It might be a reasonable simplification to require ATTR to be only
5259 `text'. One might then also require attribute lists to be stored in
5260 their canonicalized form. */
5262 static int
5263 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
5265 int ident_len;
5266 const char *p;
5268 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5269 return 0;
5271 p = IDENTIFIER_POINTER (ident);
5272 ident_len = IDENTIFIER_LENGTH (ident);
5274 if (ident_len == attr_len
5275 && strcmp (attr, p) == 0)
5276 return 1;
5278 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
5279 if (attr[0] == '_')
5281 gcc_assert (attr[1] == '_');
5282 gcc_assert (attr[attr_len - 2] == '_');
5283 gcc_assert (attr[attr_len - 1] == '_');
5284 if (ident_len == attr_len - 4
5285 && strncmp (attr + 2, p, attr_len - 4) == 0)
5286 return 1;
5288 else
5290 if (ident_len == attr_len + 4
5291 && p[0] == '_' && p[1] == '_'
5292 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5293 && strncmp (attr, p + 2, attr_len) == 0)
5294 return 1;
5297 return 0;
5300 /* Return nonzero if IDENT is a valid name for attribute ATTR,
5301 or zero if not.
5303 We try both `text' and `__text__', ATTR may be either one. */
5306 is_attribute_p (const char *attr, const_tree ident)
5308 return is_attribute_with_length_p (attr, strlen (attr), ident);
5311 /* Given an attribute name and a list of attributes, return a pointer to the
5312 attribute's list element if the attribute is part of the list, or NULL_TREE
5313 if not found. If the attribute appears more than once, this only
5314 returns the first occurrence; the TREE_CHAIN of the return value should
5315 be passed back in if further occurrences are wanted. */
5317 tree
5318 lookup_attribute (const char *attr_name, tree list)
5320 tree l;
5321 size_t attr_len = strlen (attr_name);
5323 for (l = list; l; l = TREE_CHAIN (l))
5325 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
5326 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
5327 return l;
5329 return NULL_TREE;
5332 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5333 modified list. */
5335 tree
5336 remove_attribute (const char *attr_name, tree list)
5338 tree *p;
5339 size_t attr_len = strlen (attr_name);
5341 for (p = &list; *p; )
5343 tree l = *p;
5344 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
5345 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
5346 *p = TREE_CHAIN (l);
5347 else
5348 p = &TREE_CHAIN (l);
5351 return list;
5354 /* Return an attribute list that is the union of a1 and a2. */
5356 tree
5357 merge_attributes (tree a1, tree a2)
5359 tree attributes;
5361 /* Either one unset? Take the set one. */
5363 if ((attributes = a1) == 0)
5364 attributes = a2;
5366 /* One that completely contains the other? Take it. */
5368 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5370 if (attribute_list_contained (a2, a1))
5371 attributes = a2;
5372 else
5374 /* Pick the longest list, and hang on the other list. */
5376 if (list_length (a1) < list_length (a2))
5377 attributes = a2, a2 = a1;
5379 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5381 tree a;
5382 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
5383 attributes);
5384 a != NULL_TREE && !attribute_value_equal (a, a2);
5385 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
5386 TREE_CHAIN (a)))
5388 if (a == NULL_TREE)
5390 a1 = copy_node (a2);
5391 TREE_CHAIN (a1) = attributes;
5392 attributes = a1;
5397 return attributes;
5400 /* Given types T1 and T2, merge their attributes and return
5401 the result. */
5403 tree
5404 merge_type_attributes (tree t1, tree t2)
5406 return merge_attributes (TYPE_ATTRIBUTES (t1),
5407 TYPE_ATTRIBUTES (t2));
5410 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5411 the result. */
5413 tree
5414 merge_decl_attributes (tree olddecl, tree newdecl)
5416 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5417 DECL_ATTRIBUTES (newdecl));
5420 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5422 /* Specialization of merge_decl_attributes for various Windows targets.
5424 This handles the following situation:
5426 __declspec (dllimport) int foo;
5427 int foo;
5429 The second instance of `foo' nullifies the dllimport. */
5431 tree
5432 merge_dllimport_decl_attributes (tree old, tree new_tree)
5434 tree a;
5435 int delete_dllimport_p = 1;
5437 /* What we need to do here is remove from `old' dllimport if it doesn't
5438 appear in `new'. dllimport behaves like extern: if a declaration is
5439 marked dllimport and a definition appears later, then the object
5440 is not dllimport'd. We also remove a `new' dllimport if the old list
5441 contains dllexport: dllexport always overrides dllimport, regardless
5442 of the order of declaration. */
5443 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5444 delete_dllimport_p = 0;
5445 else if (DECL_DLLIMPORT_P (new_tree)
5446 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5448 DECL_DLLIMPORT_P (new_tree) = 0;
5449 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5450 "dllimport ignored", new_tree);
5452 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5454 /* Warn about overriding a symbol that has already been used, e.g.:
5455 extern int __attribute__ ((dllimport)) foo;
5456 int* bar () {return &foo;}
5457 int foo;
5459 if (TREE_USED (old))
5461 warning (0, "%q+D redeclared without dllimport attribute "
5462 "after being referenced with dll linkage", new_tree);
5463 /* If we have used a variable's address with dllimport linkage,
5464 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5465 decl may already have had TREE_CONSTANT computed.
5466 We still remove the attribute so that assembler code refers
5467 to '&foo rather than '_imp__foo'. */
5468 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5469 DECL_DLLIMPORT_P (new_tree) = 1;
5472 /* Let an inline definition silently override the external reference,
5473 but otherwise warn about attribute inconsistency. */
5474 else if (TREE_CODE (new_tree) == VAR_DECL
5475 || !DECL_DECLARED_INLINE_P (new_tree))
5476 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5477 "previous dllimport ignored", new_tree);
5479 else
5480 delete_dllimport_p = 0;
5482 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5484 if (delete_dllimport_p)
5486 tree prev, t;
5487 const size_t attr_len = strlen ("dllimport");
5489 /* Scan the list for dllimport and delete it. */
5490 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
5492 if (is_attribute_with_length_p ("dllimport", attr_len,
5493 TREE_PURPOSE (t)))
5495 if (prev == NULL_TREE)
5496 a = TREE_CHAIN (a);
5497 else
5498 TREE_CHAIN (prev) = TREE_CHAIN (t);
5499 break;
5504 return a;
5507 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5508 struct attribute_spec.handler. */
5510 tree
5511 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5512 bool *no_add_attrs)
5514 tree node = *pnode;
5515 bool is_dllimport;
5517 /* These attributes may apply to structure and union types being created,
5518 but otherwise should pass to the declaration involved. */
5519 if (!DECL_P (node))
5521 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5522 | (int) ATTR_FLAG_ARRAY_NEXT))
5524 *no_add_attrs = true;
5525 return tree_cons (name, args, NULL_TREE);
5527 if (TREE_CODE (node) == RECORD_TYPE
5528 || TREE_CODE (node) == UNION_TYPE)
5530 node = TYPE_NAME (node);
5531 if (!node)
5532 return NULL_TREE;
5534 else
5536 warning (OPT_Wattributes, "%qE attribute ignored",
5537 name);
5538 *no_add_attrs = true;
5539 return NULL_TREE;
5543 if (TREE_CODE (node) != FUNCTION_DECL
5544 && TREE_CODE (node) != VAR_DECL
5545 && TREE_CODE (node) != TYPE_DECL)
5547 *no_add_attrs = true;
5548 warning (OPT_Wattributes, "%qE attribute ignored",
5549 name);
5550 return NULL_TREE;
5553 if (TREE_CODE (node) == TYPE_DECL
5554 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5555 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5557 *no_add_attrs = true;
5558 warning (OPT_Wattributes, "%qE attribute ignored",
5559 name);
5560 return NULL_TREE;
5563 is_dllimport = is_attribute_p ("dllimport", name);
5565 /* Report error on dllimport ambiguities seen now before they cause
5566 any damage. */
5567 if (is_dllimport)
5569 /* Honor any target-specific overrides. */
5570 if (!targetm.valid_dllimport_attribute_p (node))
5571 *no_add_attrs = true;
5573 else if (TREE_CODE (node) == FUNCTION_DECL
5574 && DECL_DECLARED_INLINE_P (node))
5576 warning (OPT_Wattributes, "inline function %q+D declared as "
5577 " dllimport: attribute ignored", node);
5578 *no_add_attrs = true;
5580 /* Like MS, treat definition of dllimported variables and
5581 non-inlined functions on declaration as syntax errors. */
5582 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5584 error ("function %q+D definition is marked dllimport", node);
5585 *no_add_attrs = true;
5588 else if (TREE_CODE (node) == VAR_DECL)
5590 if (DECL_INITIAL (node))
5592 error ("variable %q+D definition is marked dllimport",
5593 node);
5594 *no_add_attrs = true;
5597 /* `extern' needn't be specified with dllimport.
5598 Specify `extern' now and hope for the best. Sigh. */
5599 DECL_EXTERNAL (node) = 1;
5600 /* Also, implicitly give dllimport'd variables declared within
5601 a function global scope, unless declared static. */
5602 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5603 TREE_PUBLIC (node) = 1;
5606 if (*no_add_attrs == false)
5607 DECL_DLLIMPORT_P (node) = 1;
5609 else if (TREE_CODE (node) == FUNCTION_DECL
5610 && DECL_DECLARED_INLINE_P (node)
5611 && flag_keep_inline_dllexport)
5612 /* An exported function, even if inline, must be emitted. */
5613 DECL_EXTERNAL (node) = 0;
5615 /* Report error if symbol is not accessible at global scope. */
5616 if (!TREE_PUBLIC (node)
5617 && (TREE_CODE (node) == VAR_DECL
5618 || TREE_CODE (node) == FUNCTION_DECL))
5620 error ("external linkage required for symbol %q+D because of "
5621 "%qE attribute", node, name);
5622 *no_add_attrs = true;
5625 /* A dllexport'd entity must have default visibility so that other
5626 program units (shared libraries or the main executable) can see
5627 it. A dllimport'd entity must have default visibility so that
5628 the linker knows that undefined references within this program
5629 unit can be resolved by the dynamic linker. */
5630 if (!*no_add_attrs)
5632 if (DECL_VISIBILITY_SPECIFIED (node)
5633 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5634 error ("%qE implies default visibility, but %qD has already "
5635 "been declared with a different visibility",
5636 name, node);
5637 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5638 DECL_VISIBILITY_SPECIFIED (node) = 1;
5641 return NULL_TREE;
5644 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5646 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5647 of the various TYPE_QUAL values. */
5649 static void
5650 set_type_quals (tree type, int type_quals)
5652 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5653 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5654 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5655 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5658 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5660 bool
5661 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5663 return (TYPE_QUALS (cand) == type_quals
5664 && TYPE_NAME (cand) == TYPE_NAME (base)
5665 /* Apparently this is needed for Objective-C. */
5666 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5667 /* Check alignment. */
5668 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5669 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5670 TYPE_ATTRIBUTES (base)));
5673 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5675 static bool
5676 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5678 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5679 && TYPE_NAME (cand) == TYPE_NAME (base)
5680 /* Apparently this is needed for Objective-C. */
5681 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5682 /* Check alignment. */
5683 && TYPE_ALIGN (cand) == align
5684 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5685 TYPE_ATTRIBUTES (base)));
5688 /* Return a version of the TYPE, qualified as indicated by the
5689 TYPE_QUALS, if one exists. If no qualified version exists yet,
5690 return NULL_TREE. */
5692 tree
5693 get_qualified_type (tree type, int type_quals)
5695 tree t;
5697 if (TYPE_QUALS (type) == type_quals)
5698 return type;
5700 /* Search the chain of variants to see if there is already one there just
5701 like the one we need to have. If so, use that existing one. We must
5702 preserve the TYPE_NAME, since there is code that depends on this. */
5703 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5704 if (check_qualified_type (t, type, type_quals))
5705 return t;
5707 return NULL_TREE;
5710 /* Like get_qualified_type, but creates the type if it does not
5711 exist. This function never returns NULL_TREE. */
5713 tree
5714 build_qualified_type (tree type, int type_quals)
5716 tree t;
5718 /* See if we already have the appropriate qualified variant. */
5719 t = get_qualified_type (type, type_quals);
5721 /* If not, build it. */
5722 if (!t)
5724 t = build_variant_type_copy (type);
5725 set_type_quals (t, type_quals);
5727 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5728 /* Propagate structural equality. */
5729 SET_TYPE_STRUCTURAL_EQUALITY (t);
5730 else if (TYPE_CANONICAL (type) != type)
5731 /* Build the underlying canonical type, since it is different
5732 from TYPE. */
5733 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5734 type_quals);
5735 else
5736 /* T is its own canonical type. */
5737 TYPE_CANONICAL (t) = t;
5741 return t;
5744 /* Create a variant of type T with alignment ALIGN. */
5746 tree
5747 build_aligned_type (tree type, unsigned int align)
5749 tree t;
5751 if (TYPE_PACKED (type)
5752 || TYPE_ALIGN (type) == align)
5753 return type;
5755 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5756 if (check_aligned_type (t, type, align))
5757 return t;
5759 t = build_variant_type_copy (type);
5760 TYPE_ALIGN (t) = align;
5762 return t;
5765 /* Create a new distinct copy of TYPE. The new type is made its own
5766 MAIN_VARIANT. If TYPE requires structural equality checks, the
5767 resulting type requires structural equality checks; otherwise, its
5768 TYPE_CANONICAL points to itself. */
5770 tree
5771 build_distinct_type_copy (tree type)
5773 tree t = copy_node (type);
5775 TYPE_POINTER_TO (t) = 0;
5776 TYPE_REFERENCE_TO (t) = 0;
5778 /* Set the canonical type either to a new equivalence class, or
5779 propagate the need for structural equality checks. */
5780 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5781 SET_TYPE_STRUCTURAL_EQUALITY (t);
5782 else
5783 TYPE_CANONICAL (t) = t;
5785 /* Make it its own variant. */
5786 TYPE_MAIN_VARIANT (t) = t;
5787 TYPE_NEXT_VARIANT (t) = 0;
5789 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5790 whose TREE_TYPE is not t. This can also happen in the Ada
5791 frontend when using subtypes. */
5793 return t;
5796 /* Create a new variant of TYPE, equivalent but distinct. This is so
5797 the caller can modify it. TYPE_CANONICAL for the return type will
5798 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5799 are considered equal by the language itself (or that both types
5800 require structural equality checks). */
5802 tree
5803 build_variant_type_copy (tree type)
5805 tree t, m = TYPE_MAIN_VARIANT (type);
5807 t = build_distinct_type_copy (type);
5809 /* Since we're building a variant, assume that it is a non-semantic
5810 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5811 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5813 /* Add the new type to the chain of variants of TYPE. */
5814 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5815 TYPE_NEXT_VARIANT (m) = t;
5816 TYPE_MAIN_VARIANT (t) = m;
5818 return t;
5821 /* Return true if the from tree in both tree maps are equal. */
5824 tree_map_base_eq (const void *va, const void *vb)
5826 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5827 *const b = (const struct tree_map_base *) vb;
5828 return (a->from == b->from);
5831 /* Hash a from tree in a tree_base_map. */
5833 unsigned int
5834 tree_map_base_hash (const void *item)
5836 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5839 /* Return true if this tree map structure is marked for garbage collection
5840 purposes. We simply return true if the from tree is marked, so that this
5841 structure goes away when the from tree goes away. */
5844 tree_map_base_marked_p (const void *p)
5846 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5849 /* Hash a from tree in a tree_map. */
5851 unsigned int
5852 tree_map_hash (const void *item)
5854 return (((const struct tree_map *) item)->hash);
5857 /* Hash a from tree in a tree_decl_map. */
5859 unsigned int
5860 tree_decl_map_hash (const void *item)
5862 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5865 /* Return the initialization priority for DECL. */
5867 priority_type
5868 decl_init_priority_lookup (tree decl)
5870 struct tree_priority_map *h;
5871 struct tree_map_base in;
5873 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5874 in.from = decl;
5875 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5876 return h ? h->init : DEFAULT_INIT_PRIORITY;
5879 /* Return the finalization priority for DECL. */
5881 priority_type
5882 decl_fini_priority_lookup (tree decl)
5884 struct tree_priority_map *h;
5885 struct tree_map_base in;
5887 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5888 in.from = decl;
5889 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5890 return h ? h->fini : DEFAULT_INIT_PRIORITY;
5893 /* Return the initialization and finalization priority information for
5894 DECL. If there is no previous priority information, a freshly
5895 allocated structure is returned. */
5897 static struct tree_priority_map *
5898 decl_priority_info (tree decl)
5900 struct tree_priority_map in;
5901 struct tree_priority_map *h;
5902 void **loc;
5904 in.base.from = decl;
5905 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
5906 h = (struct tree_priority_map *) *loc;
5907 if (!h)
5909 h = ggc_alloc_cleared_tree_priority_map ();
5910 *loc = h;
5911 h->base.from = decl;
5912 h->init = DEFAULT_INIT_PRIORITY;
5913 h->fini = DEFAULT_INIT_PRIORITY;
5916 return h;
5919 /* Set the initialization priority for DECL to PRIORITY. */
5921 void
5922 decl_init_priority_insert (tree decl, priority_type priority)
5924 struct tree_priority_map *h;
5926 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5927 if (priority == DEFAULT_INIT_PRIORITY)
5928 return;
5929 h = decl_priority_info (decl);
5930 h->init = priority;
5933 /* Set the finalization priority for DECL to PRIORITY. */
5935 void
5936 decl_fini_priority_insert (tree decl, priority_type priority)
5938 struct tree_priority_map *h;
5940 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5941 if (priority == DEFAULT_INIT_PRIORITY)
5942 return;
5943 h = decl_priority_info (decl);
5944 h->fini = priority;
5947 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
5949 static void
5950 print_debug_expr_statistics (void)
5952 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
5953 (long) htab_size (debug_expr_for_decl),
5954 (long) htab_elements (debug_expr_for_decl),
5955 htab_collisions (debug_expr_for_decl));
5958 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
5960 static void
5961 print_value_expr_statistics (void)
5963 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
5964 (long) htab_size (value_expr_for_decl),
5965 (long) htab_elements (value_expr_for_decl),
5966 htab_collisions (value_expr_for_decl));
5969 /* Lookup a debug expression for FROM, and return it if we find one. */
5971 tree
5972 decl_debug_expr_lookup (tree from)
5974 struct tree_decl_map *h, in;
5975 in.base.from = from;
5977 h = (struct tree_decl_map *)
5978 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
5979 if (h)
5980 return h->to;
5981 return NULL_TREE;
5984 /* Insert a mapping FROM->TO in the debug expression hashtable. */
5986 void
5987 decl_debug_expr_insert (tree from, tree to)
5989 struct tree_decl_map *h;
5990 void **loc;
5992 h = ggc_alloc_tree_decl_map ();
5993 h->base.from = from;
5994 h->to = to;
5995 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
5996 INSERT);
5997 *(struct tree_decl_map **) loc = h;
6000 /* Lookup a value expression for FROM, and return it if we find one. */
6002 tree
6003 decl_value_expr_lookup (tree from)
6005 struct tree_decl_map *h, in;
6006 in.base.from = from;
6008 h = (struct tree_decl_map *)
6009 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6010 if (h)
6011 return h->to;
6012 return NULL_TREE;
6015 /* Insert a mapping FROM->TO in the value expression hashtable. */
6017 void
6018 decl_value_expr_insert (tree from, tree to)
6020 struct tree_decl_map *h;
6021 void **loc;
6023 h = ggc_alloc_tree_decl_map ();
6024 h->base.from = from;
6025 h->to = to;
6026 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6027 INSERT);
6028 *(struct tree_decl_map **) loc = h;
6031 /* Hashing of types so that we don't make duplicates.
6032 The entry point is `type_hash_canon'. */
6034 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6035 with types in the TREE_VALUE slots), by adding the hash codes
6036 of the individual types. */
6038 static unsigned int
6039 type_hash_list (const_tree list, hashval_t hashcode)
6041 const_tree tail;
6043 for (tail = list; tail; tail = TREE_CHAIN (tail))
6044 if (TREE_VALUE (tail) != error_mark_node)
6045 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6046 hashcode);
6048 return hashcode;
6051 /* These are the Hashtable callback functions. */
6053 /* Returns true iff the types are equivalent. */
6055 static int
6056 type_hash_eq (const void *va, const void *vb)
6058 const struct type_hash *const a = (const struct type_hash *) va,
6059 *const b = (const struct type_hash *) vb;
6061 /* First test the things that are the same for all types. */
6062 if (a->hash != b->hash
6063 || TREE_CODE (a->type) != TREE_CODE (b->type)
6064 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6065 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6066 TYPE_ATTRIBUTES (b->type))
6067 || (TREE_CODE (a->type) != COMPLEX_TYPE
6068 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6069 return 0;
6071 /* Be careful about comparing arrays before and after the element type
6072 has been completed; don't compare TYPE_ALIGN unless both types are
6073 complete. */
6074 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6075 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6076 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6077 return 0;
6079 switch (TREE_CODE (a->type))
6081 case VOID_TYPE:
6082 case COMPLEX_TYPE:
6083 case POINTER_TYPE:
6084 case REFERENCE_TYPE:
6085 return 1;
6087 case VECTOR_TYPE:
6088 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6090 case ENUMERAL_TYPE:
6091 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6092 && !(TYPE_VALUES (a->type)
6093 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6094 && TYPE_VALUES (b->type)
6095 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6096 && type_list_equal (TYPE_VALUES (a->type),
6097 TYPE_VALUES (b->type))))
6098 return 0;
6100 /* ... fall through ... */
6102 case INTEGER_TYPE:
6103 case REAL_TYPE:
6104 case BOOLEAN_TYPE:
6105 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6106 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6107 TYPE_MAX_VALUE (b->type)))
6108 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6109 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6110 TYPE_MIN_VALUE (b->type))));
6112 case FIXED_POINT_TYPE:
6113 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6115 case OFFSET_TYPE:
6116 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6118 case METHOD_TYPE:
6119 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6120 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6121 || (TYPE_ARG_TYPES (a->type)
6122 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6123 && TYPE_ARG_TYPES (b->type)
6124 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6125 && type_list_equal (TYPE_ARG_TYPES (a->type),
6126 TYPE_ARG_TYPES (b->type)))))
6127 break;
6128 return 0;
6129 case ARRAY_TYPE:
6130 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6132 case RECORD_TYPE:
6133 case UNION_TYPE:
6134 case QUAL_UNION_TYPE:
6135 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6136 || (TYPE_FIELDS (a->type)
6137 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6138 && TYPE_FIELDS (b->type)
6139 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6140 && type_list_equal (TYPE_FIELDS (a->type),
6141 TYPE_FIELDS (b->type))));
6143 case FUNCTION_TYPE:
6144 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6145 || (TYPE_ARG_TYPES (a->type)
6146 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6147 && TYPE_ARG_TYPES (b->type)
6148 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6149 && type_list_equal (TYPE_ARG_TYPES (a->type),
6150 TYPE_ARG_TYPES (b->type))))
6151 break;
6152 return 0;
6154 default:
6155 return 0;
6158 if (lang_hooks.types.type_hash_eq != NULL)
6159 return lang_hooks.types.type_hash_eq (a->type, b->type);
6161 return 1;
6164 /* Return the cached hash value. */
6166 static hashval_t
6167 type_hash_hash (const void *item)
6169 return ((const struct type_hash *) item)->hash;
6172 /* Look in the type hash table for a type isomorphic to TYPE.
6173 If one is found, return it. Otherwise return 0. */
6175 tree
6176 type_hash_lookup (hashval_t hashcode, tree type)
6178 struct type_hash *h, in;
6180 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6181 must call that routine before comparing TYPE_ALIGNs. */
6182 layout_type (type);
6184 in.hash = hashcode;
6185 in.type = type;
6187 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6188 hashcode);
6189 if (h)
6190 return h->type;
6191 return NULL_TREE;
6194 /* Add an entry to the type-hash-table
6195 for a type TYPE whose hash code is HASHCODE. */
6197 void
6198 type_hash_add (hashval_t hashcode, tree type)
6200 struct type_hash *h;
6201 void **loc;
6203 h = ggc_alloc_type_hash ();
6204 h->hash = hashcode;
6205 h->type = type;
6206 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6207 *loc = (void *)h;
6210 /* Given TYPE, and HASHCODE its hash code, return the canonical
6211 object for an identical type if one already exists.
6212 Otherwise, return TYPE, and record it as the canonical object.
6214 To use this function, first create a type of the sort you want.
6215 Then compute its hash code from the fields of the type that
6216 make it different from other similar types.
6217 Then call this function and use the value. */
6219 tree
6220 type_hash_canon (unsigned int hashcode, tree type)
6222 tree t1;
6224 /* The hash table only contains main variants, so ensure that's what we're
6225 being passed. */
6226 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6228 /* See if the type is in the hash table already. If so, return it.
6229 Otherwise, add the type. */
6230 t1 = type_hash_lookup (hashcode, type);
6231 if (t1 != 0)
6233 #ifdef GATHER_STATISTICS
6234 tree_code_counts[(int) TREE_CODE (type)]--;
6235 tree_node_counts[(int) t_kind]--;
6236 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6237 #endif
6238 return t1;
6240 else
6242 type_hash_add (hashcode, type);
6243 return type;
6247 /* See if the data pointed to by the type hash table is marked. We consider
6248 it marked if the type is marked or if a debug type number or symbol
6249 table entry has been made for the type. */
6251 static int
6252 type_hash_marked_p (const void *p)
6254 const_tree const type = ((const struct type_hash *) p)->type;
6256 return ggc_marked_p (type);
6259 static void
6260 print_type_hash_statistics (void)
6262 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6263 (long) htab_size (type_hash_table),
6264 (long) htab_elements (type_hash_table),
6265 htab_collisions (type_hash_table));
6268 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6269 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6270 by adding the hash codes of the individual attributes. */
6272 static unsigned int
6273 attribute_hash_list (const_tree list, hashval_t hashcode)
6275 const_tree tail;
6277 for (tail = list; tail; tail = TREE_CHAIN (tail))
6278 /* ??? Do we want to add in TREE_VALUE too? */
6279 hashcode = iterative_hash_object
6280 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
6281 return hashcode;
6284 /* Given two lists of attributes, return true if list l2 is
6285 equivalent to l1. */
6288 attribute_list_equal (const_tree l1, const_tree l2)
6290 return attribute_list_contained (l1, l2)
6291 && attribute_list_contained (l2, l1);
6294 /* Given two lists of attributes, return true if list L2 is
6295 completely contained within L1. */
6296 /* ??? This would be faster if attribute names were stored in a canonicalized
6297 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6298 must be used to show these elements are equivalent (which they are). */
6299 /* ??? It's not clear that attributes with arguments will always be handled
6300 correctly. */
6303 attribute_list_contained (const_tree l1, const_tree l2)
6305 const_tree t1, t2;
6307 /* First check the obvious, maybe the lists are identical. */
6308 if (l1 == l2)
6309 return 1;
6311 /* Maybe the lists are similar. */
6312 for (t1 = l1, t2 = l2;
6313 t1 != 0 && t2 != 0
6314 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
6315 && TREE_VALUE (t1) == TREE_VALUE (t2);
6316 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
6318 /* Maybe the lists are equal. */
6319 if (t1 == 0 && t2 == 0)
6320 return 1;
6322 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6324 const_tree attr;
6325 /* This CONST_CAST is okay because lookup_attribute does not
6326 modify its argument and the return value is assigned to a
6327 const_tree. */
6328 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
6329 CONST_CAST_TREE(l1));
6330 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6331 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
6332 TREE_CHAIN (attr)))
6335 if (attr == NULL_TREE)
6336 return 0;
6339 return 1;
6342 /* Given two lists of types
6343 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6344 return 1 if the lists contain the same types in the same order.
6345 Also, the TREE_PURPOSEs must match. */
6348 type_list_equal (const_tree l1, const_tree l2)
6350 const_tree t1, t2;
6352 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6353 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6354 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6355 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6356 && (TREE_TYPE (TREE_PURPOSE (t1))
6357 == TREE_TYPE (TREE_PURPOSE (t2))))))
6358 return 0;
6360 return t1 == t2;
6363 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6364 given by TYPE. If the argument list accepts variable arguments,
6365 then this function counts only the ordinary arguments. */
6368 type_num_arguments (const_tree type)
6370 int i = 0;
6371 tree t;
6373 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6374 /* If the function does not take a variable number of arguments,
6375 the last element in the list will have type `void'. */
6376 if (VOID_TYPE_P (TREE_VALUE (t)))
6377 break;
6378 else
6379 ++i;
6381 return i;
6384 /* Nonzero if integer constants T1 and T2
6385 represent the same constant value. */
6388 tree_int_cst_equal (const_tree t1, const_tree t2)
6390 if (t1 == t2)
6391 return 1;
6393 if (t1 == 0 || t2 == 0)
6394 return 0;
6396 if (TREE_CODE (t1) == INTEGER_CST
6397 && TREE_CODE (t2) == INTEGER_CST
6398 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6399 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6400 return 1;
6402 return 0;
6405 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6406 The precise way of comparison depends on their data type. */
6409 tree_int_cst_lt (const_tree t1, const_tree t2)
6411 if (t1 == t2)
6412 return 0;
6414 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6416 int t1_sgn = tree_int_cst_sgn (t1);
6417 int t2_sgn = tree_int_cst_sgn (t2);
6419 if (t1_sgn < t2_sgn)
6420 return 1;
6421 else if (t1_sgn > t2_sgn)
6422 return 0;
6423 /* Otherwise, both are non-negative, so we compare them as
6424 unsigned just in case one of them would overflow a signed
6425 type. */
6427 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6428 return INT_CST_LT (t1, t2);
6430 return INT_CST_LT_UNSIGNED (t1, t2);
6433 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6436 tree_int_cst_compare (const_tree t1, const_tree t2)
6438 if (tree_int_cst_lt (t1, t2))
6439 return -1;
6440 else if (tree_int_cst_lt (t2, t1))
6441 return 1;
6442 else
6443 return 0;
6446 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6447 the host. If POS is zero, the value can be represented in a single
6448 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6449 be represented in a single unsigned HOST_WIDE_INT. */
6452 host_integerp (const_tree t, int pos)
6454 if (t == NULL_TREE)
6455 return 0;
6457 return (TREE_CODE (t) == INTEGER_CST
6458 && ((TREE_INT_CST_HIGH (t) == 0
6459 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6460 || (! pos && TREE_INT_CST_HIGH (t) == -1
6461 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6462 && (!TYPE_UNSIGNED (TREE_TYPE (t))
6463 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6464 && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
6465 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6468 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6469 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6470 be non-negative. We must be able to satisfy the above conditions. */
6472 HOST_WIDE_INT
6473 tree_low_cst (const_tree t, int pos)
6475 gcc_assert (host_integerp (t, pos));
6476 return TREE_INT_CST_LOW (t);
6479 /* Return the most significant bit of the integer constant T. */
6482 tree_int_cst_msb (const_tree t)
6484 int prec;
6485 HOST_WIDE_INT h;
6486 unsigned HOST_WIDE_INT l;
6488 /* Note that using TYPE_PRECISION here is wrong. We care about the
6489 actual bits, not the (arbitrary) range of the type. */
6490 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
6491 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
6492 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
6493 return (l & 1) == 1;
6496 /* Return an indication of the sign of the integer constant T.
6497 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6498 Note that -1 will never be returned if T's type is unsigned. */
6501 tree_int_cst_sgn (const_tree t)
6503 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6504 return 0;
6505 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6506 return 1;
6507 else if (TREE_INT_CST_HIGH (t) < 0)
6508 return -1;
6509 else
6510 return 1;
6513 /* Return the minimum number of bits needed to represent VALUE in a
6514 signed or unsigned type, UNSIGNEDP says which. */
6516 unsigned int
6517 tree_int_cst_min_precision (tree value, bool unsignedp)
6519 int log;
6521 /* If the value is negative, compute its negative minus 1. The latter
6522 adjustment is because the absolute value of the largest negative value
6523 is one larger than the largest positive value. This is equivalent to
6524 a bit-wise negation, so use that operation instead. */
6526 if (tree_int_cst_sgn (value) < 0)
6527 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6529 /* Return the number of bits needed, taking into account the fact
6530 that we need one more bit for a signed than unsigned type. */
6532 if (integer_zerop (value))
6533 log = 0;
6534 else
6535 log = tree_floor_log2 (value);
6537 return log + 1 + !unsignedp;
6540 /* Compare two constructor-element-type constants. Return 1 if the lists
6541 are known to be equal; otherwise return 0. */
6544 simple_cst_list_equal (const_tree l1, const_tree l2)
6546 while (l1 != NULL_TREE && l2 != NULL_TREE)
6548 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6549 return 0;
6551 l1 = TREE_CHAIN (l1);
6552 l2 = TREE_CHAIN (l2);
6555 return l1 == l2;
6558 /* Return truthvalue of whether T1 is the same tree structure as T2.
6559 Return 1 if they are the same.
6560 Return 0 if they are understandably different.
6561 Return -1 if either contains tree structure not understood by
6562 this function. */
6565 simple_cst_equal (const_tree t1, const_tree t2)
6567 enum tree_code code1, code2;
6568 int cmp;
6569 int i;
6571 if (t1 == t2)
6572 return 1;
6573 if (t1 == 0 || t2 == 0)
6574 return 0;
6576 code1 = TREE_CODE (t1);
6577 code2 = TREE_CODE (t2);
6579 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6581 if (CONVERT_EXPR_CODE_P (code2)
6582 || code2 == NON_LVALUE_EXPR)
6583 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6584 else
6585 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6588 else if (CONVERT_EXPR_CODE_P (code2)
6589 || code2 == NON_LVALUE_EXPR)
6590 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6592 if (code1 != code2)
6593 return 0;
6595 switch (code1)
6597 case INTEGER_CST:
6598 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6599 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6601 case REAL_CST:
6602 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6604 case FIXED_CST:
6605 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6607 case STRING_CST:
6608 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6609 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6610 TREE_STRING_LENGTH (t1)));
6612 case CONSTRUCTOR:
6614 unsigned HOST_WIDE_INT idx;
6615 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
6616 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
6618 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
6619 return false;
6621 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
6622 /* ??? Should we handle also fields here? */
6623 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
6624 VEC_index (constructor_elt, v2, idx)->value))
6625 return false;
6626 return true;
6629 case SAVE_EXPR:
6630 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6632 case CALL_EXPR:
6633 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6634 if (cmp <= 0)
6635 return cmp;
6636 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6637 return 0;
6639 const_tree arg1, arg2;
6640 const_call_expr_arg_iterator iter1, iter2;
6641 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6642 arg2 = first_const_call_expr_arg (t2, &iter2);
6643 arg1 && arg2;
6644 arg1 = next_const_call_expr_arg (&iter1),
6645 arg2 = next_const_call_expr_arg (&iter2))
6647 cmp = simple_cst_equal (arg1, arg2);
6648 if (cmp <= 0)
6649 return cmp;
6651 return arg1 == arg2;
6654 case TARGET_EXPR:
6655 /* Special case: if either target is an unallocated VAR_DECL,
6656 it means that it's going to be unified with whatever the
6657 TARGET_EXPR is really supposed to initialize, so treat it
6658 as being equivalent to anything. */
6659 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6660 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6661 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6662 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6663 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6664 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6665 cmp = 1;
6666 else
6667 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6669 if (cmp <= 0)
6670 return cmp;
6672 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6674 case WITH_CLEANUP_EXPR:
6675 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6676 if (cmp <= 0)
6677 return cmp;
6679 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6681 case COMPONENT_REF:
6682 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6683 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6685 return 0;
6687 case VAR_DECL:
6688 case PARM_DECL:
6689 case CONST_DECL:
6690 case FUNCTION_DECL:
6691 return 0;
6693 default:
6694 break;
6697 /* This general rule works for most tree codes. All exceptions should be
6698 handled above. If this is a language-specific tree code, we can't
6699 trust what might be in the operand, so say we don't know
6700 the situation. */
6701 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6702 return -1;
6704 switch (TREE_CODE_CLASS (code1))
6706 case tcc_unary:
6707 case tcc_binary:
6708 case tcc_comparison:
6709 case tcc_expression:
6710 case tcc_reference:
6711 case tcc_statement:
6712 cmp = 1;
6713 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6715 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6716 if (cmp <= 0)
6717 return cmp;
6720 return cmp;
6722 default:
6723 return -1;
6727 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6728 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6729 than U, respectively. */
6732 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6734 if (tree_int_cst_sgn (t) < 0)
6735 return -1;
6736 else if (TREE_INT_CST_HIGH (t) != 0)
6737 return 1;
6738 else if (TREE_INT_CST_LOW (t) == u)
6739 return 0;
6740 else if (TREE_INT_CST_LOW (t) < u)
6741 return -1;
6742 else
6743 return 1;
6746 /* Return true if CODE represents an associative tree code. Otherwise
6747 return false. */
6748 bool
6749 associative_tree_code (enum tree_code code)
6751 switch (code)
6753 case BIT_IOR_EXPR:
6754 case BIT_AND_EXPR:
6755 case BIT_XOR_EXPR:
6756 case PLUS_EXPR:
6757 case MULT_EXPR:
6758 case MIN_EXPR:
6759 case MAX_EXPR:
6760 return true;
6762 default:
6763 break;
6765 return false;
6768 /* Return true if CODE represents a commutative tree code. Otherwise
6769 return false. */
6770 bool
6771 commutative_tree_code (enum tree_code code)
6773 switch (code)
6775 case PLUS_EXPR:
6776 case MULT_EXPR:
6777 case MIN_EXPR:
6778 case MAX_EXPR:
6779 case BIT_IOR_EXPR:
6780 case BIT_XOR_EXPR:
6781 case BIT_AND_EXPR:
6782 case NE_EXPR:
6783 case EQ_EXPR:
6784 case UNORDERED_EXPR:
6785 case ORDERED_EXPR:
6786 case UNEQ_EXPR:
6787 case LTGT_EXPR:
6788 case TRUTH_AND_EXPR:
6789 case TRUTH_XOR_EXPR:
6790 case TRUTH_OR_EXPR:
6791 return true;
6793 default:
6794 break;
6796 return false;
6799 /* Return true if CODE represents a ternary tree code for which the
6800 first two operands are commutative. Otherwise return false. */
6801 bool
6802 commutative_ternary_tree_code (enum tree_code code)
6804 switch (code)
6806 case WIDEN_MULT_PLUS_EXPR:
6807 case WIDEN_MULT_MINUS_EXPR:
6808 return true;
6810 default:
6811 break;
6813 return false;
6816 /* Generate a hash value for an expression. This can be used iteratively
6817 by passing a previous result as the VAL argument.
6819 This function is intended to produce the same hash for expressions which
6820 would compare equal using operand_equal_p. */
6822 hashval_t
6823 iterative_hash_expr (const_tree t, hashval_t val)
6825 int i;
6826 enum tree_code code;
6827 char tclass;
6829 if (t == NULL_TREE)
6830 return iterative_hash_hashval_t (0, val);
6832 code = TREE_CODE (t);
6834 switch (code)
6836 /* Alas, constants aren't shared, so we can't rely on pointer
6837 identity. */
6838 case INTEGER_CST:
6839 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
6840 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
6841 case REAL_CST:
6843 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
6845 return iterative_hash_hashval_t (val2, val);
6847 case FIXED_CST:
6849 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
6851 return iterative_hash_hashval_t (val2, val);
6853 case STRING_CST:
6854 return iterative_hash (TREE_STRING_POINTER (t),
6855 TREE_STRING_LENGTH (t), val);
6856 case COMPLEX_CST:
6857 val = iterative_hash_expr (TREE_REALPART (t), val);
6858 return iterative_hash_expr (TREE_IMAGPART (t), val);
6859 case VECTOR_CST:
6860 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
6861 case SSA_NAME:
6862 /* We can just compare by pointer. */
6863 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
6864 case PLACEHOLDER_EXPR:
6865 /* The node itself doesn't matter. */
6866 return val;
6867 case TREE_LIST:
6868 /* A list of expressions, for a CALL_EXPR or as the elements of a
6869 VECTOR_CST. */
6870 for (; t; t = TREE_CHAIN (t))
6871 val = iterative_hash_expr (TREE_VALUE (t), val);
6872 return val;
6873 case CONSTRUCTOR:
6875 unsigned HOST_WIDE_INT idx;
6876 tree field, value;
6877 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
6879 val = iterative_hash_expr (field, val);
6880 val = iterative_hash_expr (value, val);
6882 return val;
6884 case MEM_REF:
6886 /* The type of the second operand is relevant, except for
6887 its top-level qualifiers. */
6888 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
6890 val = iterative_hash_object (TYPE_HASH (type), val);
6892 /* We could use the standard hash computation from this point
6893 on. */
6894 val = iterative_hash_object (code, val);
6895 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
6896 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
6897 return val;
6899 case FUNCTION_DECL:
6900 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
6901 Otherwise nodes that compare equal according to operand_equal_p might
6902 get different hash codes. However, don't do this for machine specific
6903 or front end builtins, since the function code is overloaded in those
6904 cases. */
6905 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
6906 && built_in_decls[DECL_FUNCTION_CODE (t)])
6908 t = built_in_decls[DECL_FUNCTION_CODE (t)];
6909 code = TREE_CODE (t);
6911 /* FALL THROUGH */
6912 default:
6913 tclass = TREE_CODE_CLASS (code);
6915 if (tclass == tcc_declaration)
6917 /* DECL's have a unique ID */
6918 val = iterative_hash_host_wide_int (DECL_UID (t), val);
6920 else
6922 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
6924 val = iterative_hash_object (code, val);
6926 /* Don't hash the type, that can lead to having nodes which
6927 compare equal according to operand_equal_p, but which
6928 have different hash codes. */
6929 if (CONVERT_EXPR_CODE_P (code)
6930 || code == NON_LVALUE_EXPR)
6932 /* Make sure to include signness in the hash computation. */
6933 val += TYPE_UNSIGNED (TREE_TYPE (t));
6934 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
6937 else if (commutative_tree_code (code))
6939 /* It's a commutative expression. We want to hash it the same
6940 however it appears. We do this by first hashing both operands
6941 and then rehashing based on the order of their independent
6942 hashes. */
6943 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
6944 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
6945 hashval_t t;
6947 if (one > two)
6948 t = one, one = two, two = t;
6950 val = iterative_hash_hashval_t (one, val);
6951 val = iterative_hash_hashval_t (two, val);
6953 else
6954 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
6955 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
6957 return val;
6958 break;
6962 /* Generate a hash value for a pair of expressions. This can be used
6963 iteratively by passing a previous result as the VAL argument.
6965 The same hash value is always returned for a given pair of expressions,
6966 regardless of the order in which they are presented. This is useful in
6967 hashing the operands of commutative functions. */
6969 hashval_t
6970 iterative_hash_exprs_commutative (const_tree t1,
6971 const_tree t2, hashval_t val)
6973 hashval_t one = iterative_hash_expr (t1, 0);
6974 hashval_t two = iterative_hash_expr (t2, 0);
6975 hashval_t t;
6977 if (one > two)
6978 t = one, one = two, two = t;
6979 val = iterative_hash_hashval_t (one, val);
6980 val = iterative_hash_hashval_t (two, val);
6982 return val;
6985 /* Constructors for pointer, array and function types.
6986 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6987 constructed by language-dependent code, not here.) */
6989 /* Construct, lay out and return the type of pointers to TO_TYPE with
6990 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
6991 reference all of memory. If such a type has already been
6992 constructed, reuse it. */
6994 tree
6995 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
6996 bool can_alias_all)
6998 tree t;
7000 if (to_type == error_mark_node)
7001 return error_mark_node;
7003 /* If the pointed-to type has the may_alias attribute set, force
7004 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7005 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7006 can_alias_all = true;
7008 /* In some cases, languages will have things that aren't a POINTER_TYPE
7009 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7010 In that case, return that type without regard to the rest of our
7011 operands.
7013 ??? This is a kludge, but consistent with the way this function has
7014 always operated and there doesn't seem to be a good way to avoid this
7015 at the moment. */
7016 if (TYPE_POINTER_TO (to_type) != 0
7017 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7018 return TYPE_POINTER_TO (to_type);
7020 /* First, if we already have a type for pointers to TO_TYPE and it's
7021 the proper mode, use it. */
7022 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7023 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7024 return t;
7026 t = make_node (POINTER_TYPE);
7028 TREE_TYPE (t) = to_type;
7029 SET_TYPE_MODE (t, mode);
7030 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7031 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7032 TYPE_POINTER_TO (to_type) = t;
7034 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7035 SET_TYPE_STRUCTURAL_EQUALITY (t);
7036 else if (TYPE_CANONICAL (to_type) != to_type)
7037 TYPE_CANONICAL (t)
7038 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7039 mode, can_alias_all);
7041 /* Lay out the type. This function has many callers that are concerned
7042 with expression-construction, and this simplifies them all. */
7043 layout_type (t);
7045 return t;
7048 /* By default build pointers in ptr_mode. */
7050 tree
7051 build_pointer_type (tree to_type)
7053 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7054 : TYPE_ADDR_SPACE (to_type);
7055 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7056 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7059 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7061 tree
7062 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7063 bool can_alias_all)
7065 tree t;
7067 if (to_type == error_mark_node)
7068 return error_mark_node;
7070 /* If the pointed-to type has the may_alias attribute set, force
7071 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7072 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7073 can_alias_all = true;
7075 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7076 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7077 In that case, return that type without regard to the rest of our
7078 operands.
7080 ??? This is a kludge, but consistent with the way this function has
7081 always operated and there doesn't seem to be a good way to avoid this
7082 at the moment. */
7083 if (TYPE_REFERENCE_TO (to_type) != 0
7084 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7085 return TYPE_REFERENCE_TO (to_type);
7087 /* First, if we already have a type for pointers to TO_TYPE and it's
7088 the proper mode, use it. */
7089 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7090 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7091 return t;
7093 t = make_node (REFERENCE_TYPE);
7095 TREE_TYPE (t) = to_type;
7096 SET_TYPE_MODE (t, mode);
7097 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7098 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7099 TYPE_REFERENCE_TO (to_type) = t;
7101 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7102 SET_TYPE_STRUCTURAL_EQUALITY (t);
7103 else if (TYPE_CANONICAL (to_type) != to_type)
7104 TYPE_CANONICAL (t)
7105 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7106 mode, can_alias_all);
7108 layout_type (t);
7110 return t;
7114 /* Build the node for the type of references-to-TO_TYPE by default
7115 in ptr_mode. */
7117 tree
7118 build_reference_type (tree to_type)
7120 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7121 : TYPE_ADDR_SPACE (to_type);
7122 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7123 return build_reference_type_for_mode (to_type, pointer_mode, false);
7126 /* Build a type that is compatible with t but has no cv quals anywhere
7127 in its type, thus
7129 const char *const *const * -> char ***. */
7131 tree
7132 build_type_no_quals (tree t)
7134 switch (TREE_CODE (t))
7136 case POINTER_TYPE:
7137 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7138 TYPE_MODE (t),
7139 TYPE_REF_CAN_ALIAS_ALL (t));
7140 case REFERENCE_TYPE:
7141 return
7142 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7143 TYPE_MODE (t),
7144 TYPE_REF_CAN_ALIAS_ALL (t));
7145 default:
7146 return TYPE_MAIN_VARIANT (t);
7150 #define MAX_INT_CACHED_PREC \
7151 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7152 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7154 /* Builds a signed or unsigned integer type of precision PRECISION.
7155 Used for C bitfields whose precision does not match that of
7156 built-in target types. */
7157 tree
7158 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7159 int unsignedp)
7161 tree itype, ret;
7163 if (unsignedp)
7164 unsignedp = MAX_INT_CACHED_PREC + 1;
7166 if (precision <= MAX_INT_CACHED_PREC)
7168 itype = nonstandard_integer_type_cache[precision + unsignedp];
7169 if (itype)
7170 return itype;
7173 itype = make_node (INTEGER_TYPE);
7174 TYPE_PRECISION (itype) = precision;
7176 if (unsignedp)
7177 fixup_unsigned_type (itype);
7178 else
7179 fixup_signed_type (itype);
7181 ret = itype;
7182 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7183 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7184 if (precision <= MAX_INT_CACHED_PREC)
7185 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7187 return ret;
7190 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7191 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7192 is true, reuse such a type that has already been constructed. */
7194 static tree
7195 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7197 tree itype = make_node (INTEGER_TYPE);
7198 hashval_t hashcode = 0;
7200 TREE_TYPE (itype) = type;
7202 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7203 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7205 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7206 SET_TYPE_MODE (itype, TYPE_MODE (type));
7207 TYPE_SIZE (itype) = TYPE_SIZE (type);
7208 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7209 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7210 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7212 if (!shared)
7213 return itype;
7215 if ((TYPE_MIN_VALUE (itype)
7216 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7217 || (TYPE_MAX_VALUE (itype)
7218 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7220 /* Since we cannot reliably merge this type, we need to compare it using
7221 structural equality checks. */
7222 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7223 return itype;
7226 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7227 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7228 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7229 itype = type_hash_canon (hashcode, itype);
7231 return itype;
7234 /* Wrapper around build_range_type_1 with SHARED set to true. */
7236 tree
7237 build_range_type (tree type, tree lowval, tree highval)
7239 return build_range_type_1 (type, lowval, highval, true);
7242 /* Wrapper around build_range_type_1 with SHARED set to false. */
7244 tree
7245 build_nonshared_range_type (tree type, tree lowval, tree highval)
7247 return build_range_type_1 (type, lowval, highval, false);
7250 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7251 MAXVAL should be the maximum value in the domain
7252 (one less than the length of the array).
7254 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7255 We don't enforce this limit, that is up to caller (e.g. language front end).
7256 The limit exists because the result is a signed type and we don't handle
7257 sizes that use more than one HOST_WIDE_INT. */
7259 tree
7260 build_index_type (tree maxval)
7262 return build_range_type (sizetype, size_zero_node, maxval);
7265 /* Return true if the debug information for TYPE, a subtype, should be emitted
7266 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7267 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7268 debug info and doesn't reflect the source code. */
7270 bool
7271 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7273 tree base_type = TREE_TYPE (type), low, high;
7275 /* Subrange types have a base type which is an integral type. */
7276 if (!INTEGRAL_TYPE_P (base_type))
7277 return false;
7279 /* Get the real bounds of the subtype. */
7280 if (lang_hooks.types.get_subrange_bounds)
7281 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7282 else
7284 low = TYPE_MIN_VALUE (type);
7285 high = TYPE_MAX_VALUE (type);
7288 /* If the type and its base type have the same representation and the same
7289 name, then the type is not a subrange but a copy of the base type. */
7290 if ((TREE_CODE (base_type) == INTEGER_TYPE
7291 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7292 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7293 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7294 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7296 tree type_name = TYPE_NAME (type);
7297 tree base_type_name = TYPE_NAME (base_type);
7299 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7300 type_name = DECL_NAME (type_name);
7302 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7303 base_type_name = DECL_NAME (base_type_name);
7305 if (type_name == base_type_name)
7306 return false;
7309 if (lowval)
7310 *lowval = low;
7311 if (highval)
7312 *highval = high;
7313 return true;
7316 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7317 and number of elements specified by the range of values of INDEX_TYPE.
7318 If SHARED is true, reuse such a type that has already been constructed. */
7320 static tree
7321 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7323 tree t;
7325 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7327 error ("arrays of functions are not meaningful");
7328 elt_type = integer_type_node;
7331 t = make_node (ARRAY_TYPE);
7332 TREE_TYPE (t) = elt_type;
7333 TYPE_DOMAIN (t) = index_type;
7334 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7335 layout_type (t);
7337 /* If the element type is incomplete at this point we get marked for
7338 structural equality. Do not record these types in the canonical
7339 type hashtable. */
7340 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7341 return t;
7343 if (shared)
7345 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7346 if (index_type)
7347 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7348 t = type_hash_canon (hashcode, t);
7351 if (TYPE_CANONICAL (t) == t)
7353 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7354 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7355 SET_TYPE_STRUCTURAL_EQUALITY (t);
7356 else if (TYPE_CANONICAL (elt_type) != elt_type
7357 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7358 TYPE_CANONICAL (t)
7359 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7360 index_type
7361 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7362 shared);
7365 return t;
7368 /* Wrapper around build_array_type_1 with SHARED set to true. */
7370 tree
7371 build_array_type (tree elt_type, tree index_type)
7373 return build_array_type_1 (elt_type, index_type, true);
7376 /* Wrapper around build_array_type_1 with SHARED set to false. */
7378 tree
7379 build_nonshared_array_type (tree elt_type, tree index_type)
7381 return build_array_type_1 (elt_type, index_type, false);
7384 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7385 sizetype. */
7387 tree
7388 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7390 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7393 /* Recursively examines the array elements of TYPE, until a non-array
7394 element type is found. */
7396 tree
7397 strip_array_types (tree type)
7399 while (TREE_CODE (type) == ARRAY_TYPE)
7400 type = TREE_TYPE (type);
7402 return type;
7405 /* Computes the canonical argument types from the argument type list
7406 ARGTYPES.
7408 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7409 on entry to this function, or if any of the ARGTYPES are
7410 structural.
7412 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7413 true on entry to this function, or if any of the ARGTYPES are
7414 non-canonical.
7416 Returns a canonical argument list, which may be ARGTYPES when the
7417 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7418 true) or would not differ from ARGTYPES. */
7420 static tree
7421 maybe_canonicalize_argtypes(tree argtypes,
7422 bool *any_structural_p,
7423 bool *any_noncanonical_p)
7425 tree arg;
7426 bool any_noncanonical_argtypes_p = false;
7428 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7430 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7431 /* Fail gracefully by stating that the type is structural. */
7432 *any_structural_p = true;
7433 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7434 *any_structural_p = true;
7435 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7436 || TREE_PURPOSE (arg))
7437 /* If the argument has a default argument, we consider it
7438 non-canonical even though the type itself is canonical.
7439 That way, different variants of function and method types
7440 with default arguments will all point to the variant with
7441 no defaults as their canonical type. */
7442 any_noncanonical_argtypes_p = true;
7445 if (*any_structural_p)
7446 return argtypes;
7448 if (any_noncanonical_argtypes_p)
7450 /* Build the canonical list of argument types. */
7451 tree canon_argtypes = NULL_TREE;
7452 bool is_void = false;
7454 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7456 if (arg == void_list_node)
7457 is_void = true;
7458 else
7459 canon_argtypes = tree_cons (NULL_TREE,
7460 TYPE_CANONICAL (TREE_VALUE (arg)),
7461 canon_argtypes);
7464 canon_argtypes = nreverse (canon_argtypes);
7465 if (is_void)
7466 canon_argtypes = chainon (canon_argtypes, void_list_node);
7468 /* There is a non-canonical type. */
7469 *any_noncanonical_p = true;
7470 return canon_argtypes;
7473 /* The canonical argument types are the same as ARGTYPES. */
7474 return argtypes;
7477 /* Construct, lay out and return
7478 the type of functions returning type VALUE_TYPE
7479 given arguments of types ARG_TYPES.
7480 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7481 are data type nodes for the arguments of the function.
7482 If such a type has already been constructed, reuse it. */
7484 tree
7485 build_function_type (tree value_type, tree arg_types)
7487 tree t;
7488 hashval_t hashcode = 0;
7489 bool any_structural_p, any_noncanonical_p;
7490 tree canon_argtypes;
7492 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7494 error ("function return type cannot be function");
7495 value_type = integer_type_node;
7498 /* Make a node of the sort we want. */
7499 t = make_node (FUNCTION_TYPE);
7500 TREE_TYPE (t) = value_type;
7501 TYPE_ARG_TYPES (t) = arg_types;
7503 /* If we already have such a type, use the old one. */
7504 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7505 hashcode = type_hash_list (arg_types, hashcode);
7506 t = type_hash_canon (hashcode, t);
7508 /* Set up the canonical type. */
7509 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7510 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7511 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7512 &any_structural_p,
7513 &any_noncanonical_p);
7514 if (any_structural_p)
7515 SET_TYPE_STRUCTURAL_EQUALITY (t);
7516 else if (any_noncanonical_p)
7517 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7518 canon_argtypes);
7520 if (!COMPLETE_TYPE_P (t))
7521 layout_type (t);
7522 return t;
7525 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP. */
7527 tree
7528 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
7530 tree new_type = NULL;
7531 tree args, new_args = NULL, t;
7532 tree new_reversed;
7533 int i = 0;
7535 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7536 args = TREE_CHAIN (args), i++)
7537 if (!bitmap_bit_p (args_to_skip, i))
7538 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7540 new_reversed = nreverse (new_args);
7541 if (args)
7543 if (new_reversed)
7544 TREE_CHAIN (new_args) = void_list_node;
7545 else
7546 new_reversed = void_list_node;
7549 /* Use copy_node to preserve as much as possible from original type
7550 (debug info, attribute lists etc.)
7551 Exception is METHOD_TYPEs must have THIS argument.
7552 When we are asked to remove it, we need to build new FUNCTION_TYPE
7553 instead. */
7554 if (TREE_CODE (orig_type) != METHOD_TYPE
7555 || !bitmap_bit_p (args_to_skip, 0))
7557 new_type = build_distinct_type_copy (orig_type);
7558 TYPE_ARG_TYPES (new_type) = new_reversed;
7560 else
7562 new_type
7563 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7564 new_reversed));
7565 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7568 /* This is a new type, not a copy of an old type. Need to reassociate
7569 variants. We can handle everything except the main variant lazily. */
7570 t = TYPE_MAIN_VARIANT (orig_type);
7571 if (orig_type != t)
7573 TYPE_MAIN_VARIANT (new_type) = t;
7574 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7575 TYPE_NEXT_VARIANT (t) = new_type;
7577 else
7579 TYPE_MAIN_VARIANT (new_type) = new_type;
7580 TYPE_NEXT_VARIANT (new_type) = NULL;
7582 return new_type;
7585 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.
7587 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7588 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7589 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7591 tree
7592 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
7594 tree new_decl = copy_node (orig_decl);
7595 tree new_type;
7597 new_type = TREE_TYPE (orig_decl);
7598 if (prototype_p (new_type))
7599 new_type = build_function_type_skip_args (new_type, args_to_skip);
7600 TREE_TYPE (new_decl) = new_type;
7602 /* For declarations setting DECL_VINDEX (i.e. methods)
7603 we expect first argument to be THIS pointer. */
7604 if (bitmap_bit_p (args_to_skip, 0))
7605 DECL_VINDEX (new_decl) = NULL_TREE;
7607 /* When signature changes, we need to clear builtin info. */
7608 if (DECL_BUILT_IN (new_decl) && !bitmap_empty_p (args_to_skip))
7610 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7611 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7613 return new_decl;
7616 /* Build a function type. The RETURN_TYPE is the type returned by the
7617 function. If VAARGS is set, no void_type_node is appended to the
7618 the list. ARGP must be always be terminated be a NULL_TREE. */
7620 static tree
7621 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7623 tree t, args, last;
7625 t = va_arg (argp, tree);
7626 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7627 args = tree_cons (NULL_TREE, t, args);
7629 if (vaargs)
7631 last = args;
7632 if (args != NULL_TREE)
7633 args = nreverse (args);
7634 gcc_assert (last != void_list_node);
7636 else if (args == NULL_TREE)
7637 args = void_list_node;
7638 else
7640 last = args;
7641 args = nreverse (args);
7642 TREE_CHAIN (last) = void_list_node;
7644 args = build_function_type (return_type, args);
7646 return args;
7649 /* Build a function type. The RETURN_TYPE is the type returned by the
7650 function. If additional arguments are provided, they are
7651 additional argument types. The list of argument types must always
7652 be terminated by NULL_TREE. */
7654 tree
7655 build_function_type_list (tree return_type, ...)
7657 tree args;
7658 va_list p;
7660 va_start (p, return_type);
7661 args = build_function_type_list_1 (false, return_type, p);
7662 va_end (p);
7663 return args;
7666 /* Build a variable argument function type. The RETURN_TYPE is the
7667 type returned by the function. If additional arguments are provided,
7668 they are additional argument types. The list of argument types must
7669 always be terminated by NULL_TREE. */
7671 tree
7672 build_varargs_function_type_list (tree return_type, ...)
7674 tree args;
7675 va_list p;
7677 va_start (p, return_type);
7678 args = build_function_type_list_1 (true, return_type, p);
7679 va_end (p);
7681 return args;
7684 /* Build a function type. RETURN_TYPE is the type returned by the
7685 function; VAARGS indicates whether the function takes varargs. The
7686 function takes N named arguments, the types of which are provided in
7687 ARG_TYPES. */
7689 static tree
7690 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7691 tree *arg_types)
7693 int i;
7694 tree t = vaargs ? NULL_TREE : void_list_node;
7696 for (i = n - 1; i >= 0; i--)
7697 t = tree_cons (NULL_TREE, arg_types[i], t);
7699 return build_function_type (return_type, t);
7702 /* Build a function type. RETURN_TYPE is the type returned by the
7703 function. The function takes N named arguments, the types of which
7704 are provided in ARG_TYPES. */
7706 tree
7707 build_function_type_array (tree return_type, int n, tree *arg_types)
7709 return build_function_type_array_1 (false, return_type, n, arg_types);
7712 /* Build a variable argument function type. RETURN_TYPE is the type
7713 returned by the function. The function takes N named arguments, the
7714 types of which are provided in ARG_TYPES. */
7716 tree
7717 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7719 return build_function_type_array_1 (true, return_type, n, arg_types);
7722 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7723 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7724 for the method. An implicit additional parameter (of type
7725 pointer-to-BASETYPE) is added to the ARGTYPES. */
7727 tree
7728 build_method_type_directly (tree basetype,
7729 tree rettype,
7730 tree argtypes)
7732 tree t;
7733 tree ptype;
7734 int hashcode = 0;
7735 bool any_structural_p, any_noncanonical_p;
7736 tree canon_argtypes;
7738 /* Make a node of the sort we want. */
7739 t = make_node (METHOD_TYPE);
7741 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7742 TREE_TYPE (t) = rettype;
7743 ptype = build_pointer_type (basetype);
7745 /* The actual arglist for this function includes a "hidden" argument
7746 which is "this". Put it into the list of argument types. */
7747 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7748 TYPE_ARG_TYPES (t) = argtypes;
7750 /* If we already have such a type, use the old one. */
7751 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7752 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7753 hashcode = type_hash_list (argtypes, hashcode);
7754 t = type_hash_canon (hashcode, t);
7756 /* Set up the canonical type. */
7757 any_structural_p
7758 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7759 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7760 any_noncanonical_p
7761 = (TYPE_CANONICAL (basetype) != basetype
7762 || TYPE_CANONICAL (rettype) != rettype);
7763 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7764 &any_structural_p,
7765 &any_noncanonical_p);
7766 if (any_structural_p)
7767 SET_TYPE_STRUCTURAL_EQUALITY (t);
7768 else if (any_noncanonical_p)
7769 TYPE_CANONICAL (t)
7770 = build_method_type_directly (TYPE_CANONICAL (basetype),
7771 TYPE_CANONICAL (rettype),
7772 canon_argtypes);
7773 if (!COMPLETE_TYPE_P (t))
7774 layout_type (t);
7776 return t;
7779 /* Construct, lay out and return the type of methods belonging to class
7780 BASETYPE and whose arguments and values are described by TYPE.
7781 If that type exists already, reuse it.
7782 TYPE must be a FUNCTION_TYPE node. */
7784 tree
7785 build_method_type (tree basetype, tree type)
7787 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7789 return build_method_type_directly (basetype,
7790 TREE_TYPE (type),
7791 TYPE_ARG_TYPES (type));
7794 /* Construct, lay out and return the type of offsets to a value
7795 of type TYPE, within an object of type BASETYPE.
7796 If a suitable offset type exists already, reuse it. */
7798 tree
7799 build_offset_type (tree basetype, tree type)
7801 tree t;
7802 hashval_t hashcode = 0;
7804 /* Make a node of the sort we want. */
7805 t = make_node (OFFSET_TYPE);
7807 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7808 TREE_TYPE (t) = type;
7810 /* If we already have such a type, use the old one. */
7811 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7812 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7813 t = type_hash_canon (hashcode, t);
7815 if (!COMPLETE_TYPE_P (t))
7816 layout_type (t);
7818 if (TYPE_CANONICAL (t) == t)
7820 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7821 || TYPE_STRUCTURAL_EQUALITY_P (type))
7822 SET_TYPE_STRUCTURAL_EQUALITY (t);
7823 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7824 || TYPE_CANONICAL (type) != type)
7825 TYPE_CANONICAL (t)
7826 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7827 TYPE_CANONICAL (type));
7830 return t;
7833 /* Create a complex type whose components are COMPONENT_TYPE. */
7835 tree
7836 build_complex_type (tree component_type)
7838 tree t;
7839 hashval_t hashcode;
7841 gcc_assert (INTEGRAL_TYPE_P (component_type)
7842 || SCALAR_FLOAT_TYPE_P (component_type)
7843 || FIXED_POINT_TYPE_P (component_type));
7845 /* Make a node of the sort we want. */
7846 t = make_node (COMPLEX_TYPE);
7848 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
7850 /* If we already have such a type, use the old one. */
7851 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
7852 t = type_hash_canon (hashcode, t);
7854 if (!COMPLETE_TYPE_P (t))
7855 layout_type (t);
7857 if (TYPE_CANONICAL (t) == t)
7859 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
7860 SET_TYPE_STRUCTURAL_EQUALITY (t);
7861 else if (TYPE_CANONICAL (component_type) != component_type)
7862 TYPE_CANONICAL (t)
7863 = build_complex_type (TYPE_CANONICAL (component_type));
7866 /* We need to create a name, since complex is a fundamental type. */
7867 if (! TYPE_NAME (t))
7869 const char *name;
7870 if (component_type == char_type_node)
7871 name = "complex char";
7872 else if (component_type == signed_char_type_node)
7873 name = "complex signed char";
7874 else if (component_type == unsigned_char_type_node)
7875 name = "complex unsigned char";
7876 else if (component_type == short_integer_type_node)
7877 name = "complex short int";
7878 else if (component_type == short_unsigned_type_node)
7879 name = "complex short unsigned int";
7880 else if (component_type == integer_type_node)
7881 name = "complex int";
7882 else if (component_type == unsigned_type_node)
7883 name = "complex unsigned int";
7884 else if (component_type == long_integer_type_node)
7885 name = "complex long int";
7886 else if (component_type == long_unsigned_type_node)
7887 name = "complex long unsigned int";
7888 else if (component_type == long_long_integer_type_node)
7889 name = "complex long long int";
7890 else if (component_type == long_long_unsigned_type_node)
7891 name = "complex long long unsigned int";
7892 else
7893 name = 0;
7895 if (name != 0)
7896 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7897 get_identifier (name), t);
7900 return build_qualified_type (t, TYPE_QUALS (component_type));
7903 /* If TYPE is a real or complex floating-point type and the target
7904 does not directly support arithmetic on TYPE then return the wider
7905 type to be used for arithmetic on TYPE. Otherwise, return
7906 NULL_TREE. */
7908 tree
7909 excess_precision_type (tree type)
7911 if (flag_excess_precision != EXCESS_PRECISION_FAST)
7913 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
7914 switch (TREE_CODE (type))
7916 case REAL_TYPE:
7917 switch (flt_eval_method)
7919 case 1:
7920 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
7921 return double_type_node;
7922 break;
7923 case 2:
7924 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
7925 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
7926 return long_double_type_node;
7927 break;
7928 default:
7929 gcc_unreachable ();
7931 break;
7932 case COMPLEX_TYPE:
7933 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7934 return NULL_TREE;
7935 switch (flt_eval_method)
7937 case 1:
7938 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
7939 return complex_double_type_node;
7940 break;
7941 case 2:
7942 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
7943 || (TYPE_MODE (TREE_TYPE (type))
7944 == TYPE_MODE (double_type_node)))
7945 return complex_long_double_type_node;
7946 break;
7947 default:
7948 gcc_unreachable ();
7950 break;
7951 default:
7952 break;
7955 return NULL_TREE;
7958 /* Return OP, stripped of any conversions to wider types as much as is safe.
7959 Converting the value back to OP's type makes a value equivalent to OP.
7961 If FOR_TYPE is nonzero, we return a value which, if converted to
7962 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7964 OP must have integer, real or enumeral type. Pointers are not allowed!
7966 There are some cases where the obvious value we could return
7967 would regenerate to OP if converted to OP's type,
7968 but would not extend like OP to wider types.
7969 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7970 For example, if OP is (unsigned short)(signed char)-1,
7971 we avoid returning (signed char)-1 if FOR_TYPE is int,
7972 even though extending that to an unsigned short would regenerate OP,
7973 since the result of extending (signed char)-1 to (int)
7974 is different from (int) OP. */
7976 tree
7977 get_unwidened (tree op, tree for_type)
7979 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
7980 tree type = TREE_TYPE (op);
7981 unsigned final_prec
7982 = TYPE_PRECISION (for_type != 0 ? for_type : type);
7983 int uns
7984 = (for_type != 0 && for_type != type
7985 && final_prec > TYPE_PRECISION (type)
7986 && TYPE_UNSIGNED (type));
7987 tree win = op;
7989 while (CONVERT_EXPR_P (op))
7991 int bitschange;
7993 /* TYPE_PRECISION on vector types has different meaning
7994 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7995 so avoid them here. */
7996 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7997 break;
7999 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8000 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8002 /* Truncations are many-one so cannot be removed.
8003 Unless we are later going to truncate down even farther. */
8004 if (bitschange < 0
8005 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8006 break;
8008 /* See what's inside this conversion. If we decide to strip it,
8009 we will set WIN. */
8010 op = TREE_OPERAND (op, 0);
8012 /* If we have not stripped any zero-extensions (uns is 0),
8013 we can strip any kind of extension.
8014 If we have previously stripped a zero-extension,
8015 only zero-extensions can safely be stripped.
8016 Any extension can be stripped if the bits it would produce
8017 are all going to be discarded later by truncating to FOR_TYPE. */
8019 if (bitschange > 0)
8021 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8022 win = op;
8023 /* TYPE_UNSIGNED says whether this is a zero-extension.
8024 Let's avoid computing it if it does not affect WIN
8025 and if UNS will not be needed again. */
8026 if ((uns
8027 || CONVERT_EXPR_P (op))
8028 && TYPE_UNSIGNED (TREE_TYPE (op)))
8030 uns = 1;
8031 win = op;
8036 /* If we finally reach a constant see if it fits in for_type and
8037 in that case convert it. */
8038 if (for_type
8039 && TREE_CODE (win) == INTEGER_CST
8040 && TREE_TYPE (win) != for_type
8041 && int_fits_type_p (win, for_type))
8042 win = fold_convert (for_type, win);
8044 return win;
8047 /* Return OP or a simpler expression for a narrower value
8048 which can be sign-extended or zero-extended to give back OP.
8049 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8050 or 0 if the value should be sign-extended. */
8052 tree
8053 get_narrower (tree op, int *unsignedp_ptr)
8055 int uns = 0;
8056 int first = 1;
8057 tree win = op;
8058 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8060 while (TREE_CODE (op) == NOP_EXPR)
8062 int bitschange
8063 = (TYPE_PRECISION (TREE_TYPE (op))
8064 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8066 /* Truncations are many-one so cannot be removed. */
8067 if (bitschange < 0)
8068 break;
8070 /* See what's inside this conversion. If we decide to strip it,
8071 we will set WIN. */
8073 if (bitschange > 0)
8075 op = TREE_OPERAND (op, 0);
8076 /* An extension: the outermost one can be stripped,
8077 but remember whether it is zero or sign extension. */
8078 if (first)
8079 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8080 /* Otherwise, if a sign extension has been stripped,
8081 only sign extensions can now be stripped;
8082 if a zero extension has been stripped, only zero-extensions. */
8083 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8084 break;
8085 first = 0;
8087 else /* bitschange == 0 */
8089 /* A change in nominal type can always be stripped, but we must
8090 preserve the unsignedness. */
8091 if (first)
8092 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8093 first = 0;
8094 op = TREE_OPERAND (op, 0);
8095 /* Keep trying to narrow, but don't assign op to win if it
8096 would turn an integral type into something else. */
8097 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8098 continue;
8101 win = op;
8104 if (TREE_CODE (op) == COMPONENT_REF
8105 /* Since type_for_size always gives an integer type. */
8106 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8107 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8108 /* Ensure field is laid out already. */
8109 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8110 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8112 unsigned HOST_WIDE_INT innerprec
8113 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8114 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8115 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8116 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8118 /* We can get this structure field in a narrower type that fits it,
8119 but the resulting extension to its nominal type (a fullword type)
8120 must satisfy the same conditions as for other extensions.
8122 Do this only for fields that are aligned (not bit-fields),
8123 because when bit-field insns will be used there is no
8124 advantage in doing this. */
8126 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8127 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8128 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8129 && type != 0)
8131 if (first)
8132 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8133 win = fold_convert (type, op);
8137 *unsignedp_ptr = uns;
8138 return win;
8141 /* Returns true if integer constant C has a value that is permissible
8142 for type TYPE (an INTEGER_TYPE). */
8144 bool
8145 int_fits_type_p (const_tree c, const_tree type)
8147 tree type_low_bound, type_high_bound;
8148 bool ok_for_low_bound, ok_for_high_bound, unsc;
8149 double_int dc, dd;
8151 dc = tree_to_double_int (c);
8152 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8154 if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
8155 && TYPE_IS_SIZETYPE (TREE_TYPE (c))
8156 && unsc)
8157 /* So c is an unsigned integer whose type is sizetype and type is not.
8158 sizetype'd integers are sign extended even though they are
8159 unsigned. If the integer value fits in the lower end word of c,
8160 and if the higher end word has all its bits set to 1, that
8161 means the higher end bits are set to 1 only for sign extension.
8162 So let's convert c into an equivalent zero extended unsigned
8163 integer. */
8164 dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
8166 retry:
8167 type_low_bound = TYPE_MIN_VALUE (type);
8168 type_high_bound = TYPE_MAX_VALUE (type);
8170 /* If at least one bound of the type is a constant integer, we can check
8171 ourselves and maybe make a decision. If no such decision is possible, but
8172 this type is a subtype, try checking against that. Otherwise, use
8173 double_int_fits_to_tree_p, which checks against the precision.
8175 Compute the status for each possibly constant bound, and return if we see
8176 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8177 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8178 for "constant known to fit". */
8180 /* Check if c >= type_low_bound. */
8181 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8183 dd = tree_to_double_int (type_low_bound);
8184 if (TREE_CODE (type) == INTEGER_TYPE
8185 && TYPE_IS_SIZETYPE (type)
8186 && TYPE_UNSIGNED (type))
8187 dd = double_int_zext (dd, TYPE_PRECISION (type));
8188 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8190 int c_neg = (!unsc && double_int_negative_p (dc));
8191 int t_neg = (unsc && double_int_negative_p (dd));
8193 if (c_neg && !t_neg)
8194 return false;
8195 if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
8196 return false;
8198 else if (double_int_cmp (dc, dd, unsc) < 0)
8199 return false;
8200 ok_for_low_bound = true;
8202 else
8203 ok_for_low_bound = false;
8205 /* Check if c <= type_high_bound. */
8206 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8208 dd = tree_to_double_int (type_high_bound);
8209 if (TREE_CODE (type) == INTEGER_TYPE
8210 && TYPE_IS_SIZETYPE (type)
8211 && TYPE_UNSIGNED (type))
8212 dd = double_int_zext (dd, TYPE_PRECISION (type));
8213 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8215 int c_neg = (!unsc && double_int_negative_p (dc));
8216 int t_neg = (unsc && double_int_negative_p (dd));
8218 if (t_neg && !c_neg)
8219 return false;
8220 if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
8221 return false;
8223 else if (double_int_cmp (dc, dd, unsc) > 0)
8224 return false;
8225 ok_for_high_bound = true;
8227 else
8228 ok_for_high_bound = false;
8230 /* If the constant fits both bounds, the result is known. */
8231 if (ok_for_low_bound && ok_for_high_bound)
8232 return true;
8234 /* Perform some generic filtering which may allow making a decision
8235 even if the bounds are not constant. First, negative integers
8236 never fit in unsigned types, */
8237 if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
8238 return false;
8240 /* Second, narrower types always fit in wider ones. */
8241 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8242 return true;
8244 /* Third, unsigned integers with top bit set never fit signed types. */
8245 if (! TYPE_UNSIGNED (type) && unsc)
8247 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8248 if (prec < HOST_BITS_PER_WIDE_INT)
8250 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8251 return false;
8253 else if (((((unsigned HOST_WIDE_INT) 1)
8254 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8255 return false;
8258 /* If we haven't been able to decide at this point, there nothing more we
8259 can check ourselves here. Look at the base type if we have one and it
8260 has the same precision. */
8261 if (TREE_CODE (type) == INTEGER_TYPE
8262 && TREE_TYPE (type) != 0
8263 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8265 type = TREE_TYPE (type);
8266 goto retry;
8269 /* Or to double_int_fits_to_tree_p, if nothing else. */
8270 return double_int_fits_to_tree_p (type, dc);
8273 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8274 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8275 represented (assuming two's-complement arithmetic) within the bit
8276 precision of the type are returned instead. */
8278 void
8279 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8281 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8282 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8283 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8284 TYPE_UNSIGNED (type));
8285 else
8287 if (TYPE_UNSIGNED (type))
8288 mpz_set_ui (min, 0);
8289 else
8291 double_int mn;
8292 mn = double_int_mask (TYPE_PRECISION (type) - 1);
8293 mn = double_int_sext (double_int_add (mn, double_int_one),
8294 TYPE_PRECISION (type));
8295 mpz_set_double_int (min, mn, false);
8299 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8300 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8301 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8302 TYPE_UNSIGNED (type));
8303 else
8305 if (TYPE_UNSIGNED (type))
8306 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
8307 true);
8308 else
8309 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
8310 true);
8314 /* Return true if VAR is an automatic variable defined in function FN. */
8316 bool
8317 auto_var_in_fn_p (const_tree var, const_tree fn)
8319 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8320 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8321 || TREE_CODE (var) == PARM_DECL)
8322 && ! TREE_STATIC (var))
8323 || TREE_CODE (var) == LABEL_DECL
8324 || TREE_CODE (var) == RESULT_DECL));
8327 /* Subprogram of following function. Called by walk_tree.
8329 Return *TP if it is an automatic variable or parameter of the
8330 function passed in as DATA. */
8332 static tree
8333 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8335 tree fn = (tree) data;
8337 if (TYPE_P (*tp))
8338 *walk_subtrees = 0;
8340 else if (DECL_P (*tp)
8341 && auto_var_in_fn_p (*tp, fn))
8342 return *tp;
8344 return NULL_TREE;
8347 /* Returns true if T is, contains, or refers to a type with variable
8348 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8349 arguments, but not the return type. If FN is nonzero, only return
8350 true if a modifier of the type or position of FN is a variable or
8351 parameter inside FN.
8353 This concept is more general than that of C99 'variably modified types':
8354 in C99, a struct type is never variably modified because a VLA may not
8355 appear as a structure member. However, in GNU C code like:
8357 struct S { int i[f()]; };
8359 is valid, and other languages may define similar constructs. */
8361 bool
8362 variably_modified_type_p (tree type, tree fn)
8364 tree t;
8366 /* Test if T is either variable (if FN is zero) or an expression containing
8367 a variable in FN. */
8368 #define RETURN_TRUE_IF_VAR(T) \
8369 do { tree _t = (T); \
8370 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
8371 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8372 return true; } while (0)
8374 if (type == error_mark_node)
8375 return false;
8377 /* If TYPE itself has variable size, it is variably modified. */
8378 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8379 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8381 switch (TREE_CODE (type))
8383 case POINTER_TYPE:
8384 case REFERENCE_TYPE:
8385 case VECTOR_TYPE:
8386 if (variably_modified_type_p (TREE_TYPE (type), fn))
8387 return true;
8388 break;
8390 case FUNCTION_TYPE:
8391 case METHOD_TYPE:
8392 /* If TYPE is a function type, it is variably modified if the
8393 return type is variably modified. */
8394 if (variably_modified_type_p (TREE_TYPE (type), fn))
8395 return true;
8396 break;
8398 case INTEGER_TYPE:
8399 case REAL_TYPE:
8400 case FIXED_POINT_TYPE:
8401 case ENUMERAL_TYPE:
8402 case BOOLEAN_TYPE:
8403 /* Scalar types are variably modified if their end points
8404 aren't constant. */
8405 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8406 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8407 break;
8409 case RECORD_TYPE:
8410 case UNION_TYPE:
8411 case QUAL_UNION_TYPE:
8412 /* We can't see if any of the fields are variably-modified by the
8413 definition we normally use, since that would produce infinite
8414 recursion via pointers. */
8415 /* This is variably modified if some field's type is. */
8416 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8417 if (TREE_CODE (t) == FIELD_DECL)
8419 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8420 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8421 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8423 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8424 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8426 break;
8428 case ARRAY_TYPE:
8429 /* Do not call ourselves to avoid infinite recursion. This is
8430 variably modified if the element type is. */
8431 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8432 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8433 break;
8435 default:
8436 break;
8439 /* The current language may have other cases to check, but in general,
8440 all other types are not variably modified. */
8441 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8443 #undef RETURN_TRUE_IF_VAR
8446 /* Given a DECL or TYPE, return the scope in which it was declared, or
8447 NULL_TREE if there is no containing scope. */
8449 tree
8450 get_containing_scope (const_tree t)
8452 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8455 /* Return the innermost context enclosing DECL that is
8456 a FUNCTION_DECL, or zero if none. */
8458 tree
8459 decl_function_context (const_tree decl)
8461 tree context;
8463 if (TREE_CODE (decl) == ERROR_MARK)
8464 return 0;
8466 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8467 where we look up the function at runtime. Such functions always take
8468 a first argument of type 'pointer to real context'.
8470 C++ should really be fixed to use DECL_CONTEXT for the real context,
8471 and use something else for the "virtual context". */
8472 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8473 context
8474 = TYPE_MAIN_VARIANT
8475 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8476 else
8477 context = DECL_CONTEXT (decl);
8479 while (context && TREE_CODE (context) != FUNCTION_DECL)
8481 if (TREE_CODE (context) == BLOCK)
8482 context = BLOCK_SUPERCONTEXT (context);
8483 else
8484 context = get_containing_scope (context);
8487 return context;
8490 /* Return the innermost context enclosing DECL that is
8491 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8492 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8494 tree
8495 decl_type_context (const_tree decl)
8497 tree context = DECL_CONTEXT (decl);
8499 while (context)
8500 switch (TREE_CODE (context))
8502 case NAMESPACE_DECL:
8503 case TRANSLATION_UNIT_DECL:
8504 return NULL_TREE;
8506 case RECORD_TYPE:
8507 case UNION_TYPE:
8508 case QUAL_UNION_TYPE:
8509 return context;
8511 case TYPE_DECL:
8512 case FUNCTION_DECL:
8513 context = DECL_CONTEXT (context);
8514 break;
8516 case BLOCK:
8517 context = BLOCK_SUPERCONTEXT (context);
8518 break;
8520 default:
8521 gcc_unreachable ();
8524 return NULL_TREE;
8527 /* CALL is a CALL_EXPR. Return the declaration for the function
8528 called, or NULL_TREE if the called function cannot be
8529 determined. */
8531 tree
8532 get_callee_fndecl (const_tree call)
8534 tree addr;
8536 if (call == error_mark_node)
8537 return error_mark_node;
8539 /* It's invalid to call this function with anything but a
8540 CALL_EXPR. */
8541 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8543 /* The first operand to the CALL is the address of the function
8544 called. */
8545 addr = CALL_EXPR_FN (call);
8547 STRIP_NOPS (addr);
8549 /* If this is a readonly function pointer, extract its initial value. */
8550 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8551 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8552 && DECL_INITIAL (addr))
8553 addr = DECL_INITIAL (addr);
8555 /* If the address is just `&f' for some function `f', then we know
8556 that `f' is being called. */
8557 if (TREE_CODE (addr) == ADDR_EXPR
8558 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8559 return TREE_OPERAND (addr, 0);
8561 /* We couldn't figure out what was being called. */
8562 return NULL_TREE;
8565 /* Print debugging information about tree nodes generated during the compile,
8566 and any language-specific information. */
8568 void
8569 dump_tree_statistics (void)
8571 #ifdef GATHER_STATISTICS
8572 int i;
8573 int total_nodes, total_bytes;
8574 #endif
8576 fprintf (stderr, "\n??? tree nodes created\n\n");
8577 #ifdef GATHER_STATISTICS
8578 fprintf (stderr, "Kind Nodes Bytes\n");
8579 fprintf (stderr, "---------------------------------------\n");
8580 total_nodes = total_bytes = 0;
8581 for (i = 0; i < (int) all_kinds; i++)
8583 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8584 tree_node_counts[i], tree_node_sizes[i]);
8585 total_nodes += tree_node_counts[i];
8586 total_bytes += tree_node_sizes[i];
8588 fprintf (stderr, "---------------------------------------\n");
8589 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8590 fprintf (stderr, "---------------------------------------\n");
8591 fprintf (stderr, "Code Nodes\n");
8592 fprintf (stderr, "----------------------------\n");
8593 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8594 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8595 fprintf (stderr, "----------------------------\n");
8596 ssanames_print_statistics ();
8597 phinodes_print_statistics ();
8598 #else
8599 fprintf (stderr, "(No per-node statistics)\n");
8600 #endif
8601 print_type_hash_statistics ();
8602 print_debug_expr_statistics ();
8603 print_value_expr_statistics ();
8604 lang_hooks.print_statistics ();
8607 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8609 /* Generate a crc32 of a byte. */
8611 unsigned
8612 crc32_byte (unsigned chksum, char byte)
8614 unsigned value = (unsigned) byte << 24;
8615 unsigned ix;
8617 for (ix = 8; ix--; value <<= 1)
8619 unsigned feedback;
8621 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8622 chksum <<= 1;
8623 chksum ^= feedback;
8625 return chksum;
8629 /* Generate a crc32 of a string. */
8631 unsigned
8632 crc32_string (unsigned chksum, const char *string)
8636 chksum = crc32_byte (chksum, *string);
8638 while (*string++);
8639 return chksum;
8642 /* P is a string that will be used in a symbol. Mask out any characters
8643 that are not valid in that context. */
8645 void
8646 clean_symbol_name (char *p)
8648 for (; *p; p++)
8649 if (! (ISALNUM (*p)
8650 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8651 || *p == '$'
8652 #endif
8653 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8654 || *p == '.'
8655 #endif
8657 *p = '_';
8660 /* Generate a name for a special-purpose function.
8661 The generated name may need to be unique across the whole link.
8662 Changes to this function may also require corresponding changes to
8663 xstrdup_mask_random.
8664 TYPE is some string to identify the purpose of this function to the
8665 linker or collect2; it must start with an uppercase letter,
8666 one of:
8667 I - for constructors
8668 D - for destructors
8669 N - for C++ anonymous namespaces
8670 F - for DWARF unwind frame information. */
8672 tree
8673 get_file_function_name (const char *type)
8675 char *buf;
8676 const char *p;
8677 char *q;
8679 /* If we already have a name we know to be unique, just use that. */
8680 if (first_global_object_name)
8681 p = q = ASTRDUP (first_global_object_name);
8682 /* If the target is handling the constructors/destructors, they
8683 will be local to this file and the name is only necessary for
8684 debugging purposes.
8685 We also assign sub_I and sub_D sufixes to constructors called from
8686 the global static constructors. These are always local. */
8687 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8688 || (strncmp (type, "sub_", 4) == 0
8689 && (type[4] == 'I' || type[4] == 'D')))
8691 const char *file = main_input_filename;
8692 if (! file)
8693 file = input_filename;
8694 /* Just use the file's basename, because the full pathname
8695 might be quite long. */
8696 p = q = ASTRDUP (lbasename (file));
8698 else
8700 /* Otherwise, the name must be unique across the entire link.
8701 We don't have anything that we know to be unique to this translation
8702 unit, so use what we do have and throw in some randomness. */
8703 unsigned len;
8704 const char *name = weak_global_object_name;
8705 const char *file = main_input_filename;
8707 if (! name)
8708 name = "";
8709 if (! file)
8710 file = input_filename;
8712 len = strlen (file);
8713 q = (char *) alloca (9 * 2 + len + 1);
8714 memcpy (q, file, len + 1);
8716 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
8717 crc32_string (0, get_random_seed (false)));
8719 p = q;
8722 clean_symbol_name (q);
8723 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8724 + strlen (type));
8726 /* Set up the name of the file-level functions we may need.
8727 Use a global object (which is already required to be unique over
8728 the program) rather than the file name (which imposes extra
8729 constraints). */
8730 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8732 return get_identifier (buf);
8735 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8737 /* Complain that the tree code of NODE does not match the expected 0
8738 terminated list of trailing codes. The trailing code list can be
8739 empty, for a more vague error message. FILE, LINE, and FUNCTION
8740 are of the caller. */
8742 void
8743 tree_check_failed (const_tree node, const char *file,
8744 int line, const char *function, ...)
8746 va_list args;
8747 const char *buffer;
8748 unsigned length = 0;
8749 int code;
8751 va_start (args, function);
8752 while ((code = va_arg (args, int)))
8753 length += 4 + strlen (tree_code_name[code]);
8754 va_end (args);
8755 if (length)
8757 char *tmp;
8758 va_start (args, function);
8759 length += strlen ("expected ");
8760 buffer = tmp = (char *) alloca (length);
8761 length = 0;
8762 while ((code = va_arg (args, int)))
8764 const char *prefix = length ? " or " : "expected ";
8766 strcpy (tmp + length, prefix);
8767 length += strlen (prefix);
8768 strcpy (tmp + length, tree_code_name[code]);
8769 length += strlen (tree_code_name[code]);
8771 va_end (args);
8773 else
8774 buffer = "unexpected node";
8776 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8777 buffer, tree_code_name[TREE_CODE (node)],
8778 function, trim_filename (file), line);
8781 /* Complain that the tree code of NODE does match the expected 0
8782 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8783 the caller. */
8785 void
8786 tree_not_check_failed (const_tree node, const char *file,
8787 int line, const char *function, ...)
8789 va_list args;
8790 char *buffer;
8791 unsigned length = 0;
8792 int code;
8794 va_start (args, function);
8795 while ((code = va_arg (args, int)))
8796 length += 4 + strlen (tree_code_name[code]);
8797 va_end (args);
8798 va_start (args, function);
8799 buffer = (char *) alloca (length);
8800 length = 0;
8801 while ((code = va_arg (args, int)))
8803 if (length)
8805 strcpy (buffer + length, " or ");
8806 length += 4;
8808 strcpy (buffer + length, tree_code_name[code]);
8809 length += strlen (tree_code_name[code]);
8811 va_end (args);
8813 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8814 buffer, tree_code_name[TREE_CODE (node)],
8815 function, trim_filename (file), line);
8818 /* Similar to tree_check_failed, except that we check for a class of tree
8819 code, given in CL. */
8821 void
8822 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8823 const char *file, int line, const char *function)
8825 internal_error
8826 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8827 TREE_CODE_CLASS_STRING (cl),
8828 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8829 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8832 /* Similar to tree_check_failed, except that instead of specifying a
8833 dozen codes, use the knowledge that they're all sequential. */
8835 void
8836 tree_range_check_failed (const_tree node, const char *file, int line,
8837 const char *function, enum tree_code c1,
8838 enum tree_code c2)
8840 char *buffer;
8841 unsigned length = 0;
8842 unsigned int c;
8844 for (c = c1; c <= c2; ++c)
8845 length += 4 + strlen (tree_code_name[c]);
8847 length += strlen ("expected ");
8848 buffer = (char *) alloca (length);
8849 length = 0;
8851 for (c = c1; c <= c2; ++c)
8853 const char *prefix = length ? " or " : "expected ";
8855 strcpy (buffer + length, prefix);
8856 length += strlen (prefix);
8857 strcpy (buffer + length, tree_code_name[c]);
8858 length += strlen (tree_code_name[c]);
8861 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8862 buffer, tree_code_name[TREE_CODE (node)],
8863 function, trim_filename (file), line);
8867 /* Similar to tree_check_failed, except that we check that a tree does
8868 not have the specified code, given in CL. */
8870 void
8871 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
8872 const char *file, int line, const char *function)
8874 internal_error
8875 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
8876 TREE_CODE_CLASS_STRING (cl),
8877 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8878 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8882 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
8884 void
8885 omp_clause_check_failed (const_tree node, const char *file, int line,
8886 const char *function, enum omp_clause_code code)
8888 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
8889 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
8890 function, trim_filename (file), line);
8894 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
8896 void
8897 omp_clause_range_check_failed (const_tree node, const char *file, int line,
8898 const char *function, enum omp_clause_code c1,
8899 enum omp_clause_code c2)
8901 char *buffer;
8902 unsigned length = 0;
8903 unsigned int c;
8905 for (c = c1; c <= c2; ++c)
8906 length += 4 + strlen (omp_clause_code_name[c]);
8908 length += strlen ("expected ");
8909 buffer = (char *) alloca (length);
8910 length = 0;
8912 for (c = c1; c <= c2; ++c)
8914 const char *prefix = length ? " or " : "expected ";
8916 strcpy (buffer + length, prefix);
8917 length += strlen (prefix);
8918 strcpy (buffer + length, omp_clause_code_name[c]);
8919 length += strlen (omp_clause_code_name[c]);
8922 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8923 buffer, omp_clause_code_name[TREE_CODE (node)],
8924 function, trim_filename (file), line);
8928 #undef DEFTREESTRUCT
8929 #define DEFTREESTRUCT(VAL, NAME) NAME,
8931 static const char *ts_enum_names[] = {
8932 #include "treestruct.def"
8934 #undef DEFTREESTRUCT
8936 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
8938 /* Similar to tree_class_check_failed, except that we check for
8939 whether CODE contains the tree structure identified by EN. */
8941 void
8942 tree_contains_struct_check_failed (const_tree node,
8943 const enum tree_node_structure_enum en,
8944 const char *file, int line,
8945 const char *function)
8947 internal_error
8948 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
8949 TS_ENUM_NAME(en),
8950 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8954 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
8955 (dynamically sized) vector. */
8957 void
8958 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
8959 const char *function)
8961 internal_error
8962 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
8963 idx + 1, len, function, trim_filename (file), line);
8966 /* Similar to above, except that the check is for the bounds of the operand
8967 vector of an expression node EXP. */
8969 void
8970 tree_operand_check_failed (int idx, const_tree exp, const char *file,
8971 int line, const char *function)
8973 int code = TREE_CODE (exp);
8974 internal_error
8975 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
8976 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
8977 function, trim_filename (file), line);
8980 /* Similar to above, except that the check is for the number of
8981 operands of an OMP_CLAUSE node. */
8983 void
8984 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
8985 int line, const char *function)
8987 internal_error
8988 ("tree check: accessed operand %d of omp_clause %s with %d operands "
8989 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
8990 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
8991 trim_filename (file), line);
8993 #endif /* ENABLE_TREE_CHECKING */
8995 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
8996 and mapped to the machine mode MODE. Initialize its fields and build
8997 the information necessary for debugging output. */
8999 static tree
9000 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9002 tree t;
9003 hashval_t hashcode = 0;
9005 t = make_node (VECTOR_TYPE);
9006 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9007 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9008 SET_TYPE_MODE (t, mode);
9010 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9011 SET_TYPE_STRUCTURAL_EQUALITY (t);
9012 else if (TYPE_CANONICAL (innertype) != innertype
9013 || mode != VOIDmode)
9014 TYPE_CANONICAL (t)
9015 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9017 layout_type (t);
9019 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9020 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9021 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9022 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9023 t = type_hash_canon (hashcode, t);
9025 /* We have built a main variant, based on the main variant of the
9026 inner type. Use it to build the variant we return. */
9027 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9028 && TREE_TYPE (t) != innertype)
9029 return build_type_attribute_qual_variant (t,
9030 TYPE_ATTRIBUTES (innertype),
9031 TYPE_QUALS (innertype));
9033 return t;
9036 static tree
9037 make_or_reuse_type (unsigned size, int unsignedp)
9039 if (size == INT_TYPE_SIZE)
9040 return unsignedp ? unsigned_type_node : integer_type_node;
9041 if (size == CHAR_TYPE_SIZE)
9042 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9043 if (size == SHORT_TYPE_SIZE)
9044 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9045 if (size == LONG_TYPE_SIZE)
9046 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9047 if (size == LONG_LONG_TYPE_SIZE)
9048 return (unsignedp ? long_long_unsigned_type_node
9049 : long_long_integer_type_node);
9050 if (size == 128 && int128_integer_type_node)
9051 return (unsignedp ? int128_unsigned_type_node
9052 : int128_integer_type_node);
9054 if (unsignedp)
9055 return make_unsigned_type (size);
9056 else
9057 return make_signed_type (size);
9060 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9062 static tree
9063 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9065 if (satp)
9067 if (size == SHORT_FRACT_TYPE_SIZE)
9068 return unsignedp ? sat_unsigned_short_fract_type_node
9069 : sat_short_fract_type_node;
9070 if (size == FRACT_TYPE_SIZE)
9071 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9072 if (size == LONG_FRACT_TYPE_SIZE)
9073 return unsignedp ? sat_unsigned_long_fract_type_node
9074 : sat_long_fract_type_node;
9075 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9076 return unsignedp ? sat_unsigned_long_long_fract_type_node
9077 : sat_long_long_fract_type_node;
9079 else
9081 if (size == SHORT_FRACT_TYPE_SIZE)
9082 return unsignedp ? unsigned_short_fract_type_node
9083 : short_fract_type_node;
9084 if (size == FRACT_TYPE_SIZE)
9085 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9086 if (size == LONG_FRACT_TYPE_SIZE)
9087 return unsignedp ? unsigned_long_fract_type_node
9088 : long_fract_type_node;
9089 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9090 return unsignedp ? unsigned_long_long_fract_type_node
9091 : long_long_fract_type_node;
9094 return make_fract_type (size, unsignedp, satp);
9097 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9099 static tree
9100 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9102 if (satp)
9104 if (size == SHORT_ACCUM_TYPE_SIZE)
9105 return unsignedp ? sat_unsigned_short_accum_type_node
9106 : sat_short_accum_type_node;
9107 if (size == ACCUM_TYPE_SIZE)
9108 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9109 if (size == LONG_ACCUM_TYPE_SIZE)
9110 return unsignedp ? sat_unsigned_long_accum_type_node
9111 : sat_long_accum_type_node;
9112 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9113 return unsignedp ? sat_unsigned_long_long_accum_type_node
9114 : sat_long_long_accum_type_node;
9116 else
9118 if (size == SHORT_ACCUM_TYPE_SIZE)
9119 return unsignedp ? unsigned_short_accum_type_node
9120 : short_accum_type_node;
9121 if (size == ACCUM_TYPE_SIZE)
9122 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9123 if (size == LONG_ACCUM_TYPE_SIZE)
9124 return unsignedp ? unsigned_long_accum_type_node
9125 : long_accum_type_node;
9126 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9127 return unsignedp ? unsigned_long_long_accum_type_node
9128 : long_long_accum_type_node;
9131 return make_accum_type (size, unsignedp, satp);
9134 /* Create nodes for all integer types (and error_mark_node) using the sizes
9135 of C datatypes. The caller should call set_sizetype soon after calling
9136 this function to select one of the types as sizetype. */
9138 void
9139 build_common_tree_nodes (bool signed_char)
9141 error_mark_node = make_node (ERROR_MARK);
9142 TREE_TYPE (error_mark_node) = error_mark_node;
9144 initialize_sizetypes ();
9146 /* Define both `signed char' and `unsigned char'. */
9147 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9148 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9149 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9150 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9152 /* Define `char', which is like either `signed char' or `unsigned char'
9153 but not the same as either. */
9154 char_type_node
9155 = (signed_char
9156 ? make_signed_type (CHAR_TYPE_SIZE)
9157 : make_unsigned_type (CHAR_TYPE_SIZE));
9158 TYPE_STRING_FLAG (char_type_node) = 1;
9160 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9161 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9162 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9163 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9164 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9165 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9166 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9167 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9168 #if HOST_BITS_PER_WIDE_INT >= 64
9169 /* TODO: This isn't correct, but as logic depends at the moment on
9170 host's instead of target's wide-integer.
9171 If there is a target not supporting TImode, but has an 128-bit
9172 integer-scalar register, this target check needs to be adjusted. */
9173 if (targetm.scalar_mode_supported_p (TImode))
9175 int128_integer_type_node = make_signed_type (128);
9176 int128_unsigned_type_node = make_unsigned_type (128);
9178 #endif
9179 /* Define a boolean type. This type only represents boolean values but
9180 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9181 Front ends which want to override this size (i.e. Java) can redefine
9182 boolean_type_node before calling build_common_tree_nodes_2. */
9183 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9184 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9185 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9186 TYPE_PRECISION (boolean_type_node) = 1;
9188 /* Fill in the rest of the sized types. Reuse existing type nodes
9189 when possible. */
9190 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9191 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9192 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9193 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9194 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9196 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9197 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9198 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9199 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9200 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9202 access_public_node = get_identifier ("public");
9203 access_protected_node = get_identifier ("protected");
9204 access_private_node = get_identifier ("private");
9207 /* Call this function after calling build_common_tree_nodes and set_sizetype.
9208 It will create several other common tree nodes. */
9210 void
9211 build_common_tree_nodes_2 (int short_double)
9213 /* Define these next since types below may used them. */
9214 integer_zero_node = build_int_cst (integer_type_node, 0);
9215 integer_one_node = build_int_cst (integer_type_node, 1);
9216 integer_three_node = build_int_cst (integer_type_node, 3);
9217 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9219 size_zero_node = size_int (0);
9220 size_one_node = size_int (1);
9221 bitsize_zero_node = bitsize_int (0);
9222 bitsize_one_node = bitsize_int (1);
9223 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9225 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9226 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9228 void_type_node = make_node (VOID_TYPE);
9229 layout_type (void_type_node);
9231 /* We are not going to have real types in C with less than byte alignment,
9232 so we might as well not have any types that claim to have it. */
9233 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9234 TYPE_USER_ALIGN (void_type_node) = 0;
9236 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9237 layout_type (TREE_TYPE (null_pointer_node));
9239 ptr_type_node = build_pointer_type (void_type_node);
9240 const_ptr_type_node
9241 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9242 fileptr_type_node = ptr_type_node;
9244 float_type_node = make_node (REAL_TYPE);
9245 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9246 layout_type (float_type_node);
9248 double_type_node = make_node (REAL_TYPE);
9249 if (short_double)
9250 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9251 else
9252 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9253 layout_type (double_type_node);
9255 long_double_type_node = make_node (REAL_TYPE);
9256 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9257 layout_type (long_double_type_node);
9259 float_ptr_type_node = build_pointer_type (float_type_node);
9260 double_ptr_type_node = build_pointer_type (double_type_node);
9261 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9262 integer_ptr_type_node = build_pointer_type (integer_type_node);
9264 /* Fixed size integer types. */
9265 uint32_type_node = build_nonstandard_integer_type (32, true);
9266 uint64_type_node = build_nonstandard_integer_type (64, true);
9268 /* Decimal float types. */
9269 dfloat32_type_node = make_node (REAL_TYPE);
9270 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9271 layout_type (dfloat32_type_node);
9272 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9273 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9275 dfloat64_type_node = make_node (REAL_TYPE);
9276 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9277 layout_type (dfloat64_type_node);
9278 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9279 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9281 dfloat128_type_node = make_node (REAL_TYPE);
9282 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9283 layout_type (dfloat128_type_node);
9284 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9285 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9287 complex_integer_type_node = build_complex_type (integer_type_node);
9288 complex_float_type_node = build_complex_type (float_type_node);
9289 complex_double_type_node = build_complex_type (double_type_node);
9290 complex_long_double_type_node = build_complex_type (long_double_type_node);
9292 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9293 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9294 sat_ ## KIND ## _type_node = \
9295 make_sat_signed_ ## KIND ## _type (SIZE); \
9296 sat_unsigned_ ## KIND ## _type_node = \
9297 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9298 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9299 unsigned_ ## KIND ## _type_node = \
9300 make_unsigned_ ## KIND ## _type (SIZE);
9302 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9303 sat_ ## WIDTH ## KIND ## _type_node = \
9304 make_sat_signed_ ## KIND ## _type (SIZE); \
9305 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9306 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9307 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9308 unsigned_ ## WIDTH ## KIND ## _type_node = \
9309 make_unsigned_ ## KIND ## _type (SIZE);
9311 /* Make fixed-point type nodes based on four different widths. */
9312 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9313 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9314 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9315 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9316 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9318 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9319 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9320 NAME ## _type_node = \
9321 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9322 u ## NAME ## _type_node = \
9323 make_or_reuse_unsigned_ ## KIND ## _type \
9324 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9325 sat_ ## NAME ## _type_node = \
9326 make_or_reuse_sat_signed_ ## KIND ## _type \
9327 (GET_MODE_BITSIZE (MODE ## mode)); \
9328 sat_u ## NAME ## _type_node = \
9329 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9330 (GET_MODE_BITSIZE (U ## MODE ## mode));
9332 /* Fixed-point type and mode nodes. */
9333 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9334 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9335 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9336 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9337 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9338 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9339 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9340 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9341 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9342 MAKE_FIXED_MODE_NODE (accum, da, DA)
9343 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9346 tree t = targetm.build_builtin_va_list ();
9348 /* Many back-ends define record types without setting TYPE_NAME.
9349 If we copied the record type here, we'd keep the original
9350 record type without a name. This breaks name mangling. So,
9351 don't copy record types and let c_common_nodes_and_builtins()
9352 declare the type to be __builtin_va_list. */
9353 if (TREE_CODE (t) != RECORD_TYPE)
9354 t = build_variant_type_copy (t);
9356 va_list_type_node = t;
9360 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9362 static void
9363 local_define_builtin (const char *name, tree type, enum built_in_function code,
9364 const char *library_name, int ecf_flags)
9366 tree decl;
9368 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9369 library_name, NULL_TREE);
9370 if (ecf_flags & ECF_CONST)
9371 TREE_READONLY (decl) = 1;
9372 if (ecf_flags & ECF_PURE)
9373 DECL_PURE_P (decl) = 1;
9374 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
9375 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9376 if (ecf_flags & ECF_NORETURN)
9377 TREE_THIS_VOLATILE (decl) = 1;
9378 if (ecf_flags & ECF_NOTHROW)
9379 TREE_NOTHROW (decl) = 1;
9380 if (ecf_flags & ECF_MALLOC)
9381 DECL_IS_MALLOC (decl) = 1;
9382 if (ecf_flags & ECF_LEAF)
9383 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9384 NULL, DECL_ATTRIBUTES (decl));
9386 built_in_decls[code] = decl;
9387 implicit_built_in_decls[code] = decl;
9390 /* Call this function after instantiating all builtins that the language
9391 front end cares about. This will build the rest of the builtins that
9392 are relied upon by the tree optimizers and the middle-end. */
9394 void
9395 build_common_builtin_nodes (void)
9397 tree tmp, ftype;
9399 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
9400 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
9402 ftype = build_function_type_list (ptr_type_node,
9403 ptr_type_node, const_ptr_type_node,
9404 size_type_node, NULL_TREE);
9406 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
9407 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9408 "memcpy", ECF_NOTHROW | ECF_LEAF);
9409 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
9410 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9411 "memmove", ECF_NOTHROW | ECF_LEAF);
9414 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
9416 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9417 const_ptr_type_node, size_type_node,
9418 NULL_TREE);
9419 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9420 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9423 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
9425 ftype = build_function_type_list (ptr_type_node,
9426 ptr_type_node, integer_type_node,
9427 size_type_node, NULL_TREE);
9428 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9429 "memset", ECF_NOTHROW | ECF_LEAF);
9432 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
9434 ftype = build_function_type_list (ptr_type_node,
9435 size_type_node, NULL_TREE);
9436 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9437 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9440 /* If we're checking the stack, `alloca' can throw. */
9441 if (flag_stack_check)
9442 TREE_NOTHROW (built_in_decls[BUILT_IN_ALLOCA]) = 0;
9444 ftype = build_function_type_list (void_type_node,
9445 ptr_type_node, ptr_type_node,
9446 ptr_type_node, NULL_TREE);
9447 local_define_builtin ("__builtin_init_trampoline", ftype,
9448 BUILT_IN_INIT_TRAMPOLINE,
9449 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9451 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9452 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9453 BUILT_IN_ADJUST_TRAMPOLINE,
9454 "__builtin_adjust_trampoline",
9455 ECF_CONST | ECF_NOTHROW);
9457 ftype = build_function_type_list (void_type_node,
9458 ptr_type_node, ptr_type_node, NULL_TREE);
9459 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9460 BUILT_IN_NONLOCAL_GOTO,
9461 "__builtin_nonlocal_goto",
9462 ECF_NORETURN | ECF_NOTHROW);
9464 ftype = build_function_type_list (void_type_node,
9465 ptr_type_node, ptr_type_node, NULL_TREE);
9466 local_define_builtin ("__builtin_setjmp_setup", ftype,
9467 BUILT_IN_SETJMP_SETUP,
9468 "__builtin_setjmp_setup", ECF_NOTHROW);
9470 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9471 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9472 BUILT_IN_SETJMP_DISPATCHER,
9473 "__builtin_setjmp_dispatcher",
9474 ECF_PURE | ECF_NOTHROW);
9476 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9477 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9478 BUILT_IN_SETJMP_RECEIVER,
9479 "__builtin_setjmp_receiver", ECF_NOTHROW);
9481 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9482 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9483 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9485 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9486 local_define_builtin ("__builtin_stack_restore", ftype,
9487 BUILT_IN_STACK_RESTORE,
9488 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9490 /* If there's a possibility that we might use the ARM EABI, build the
9491 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9492 if (targetm.arm_eabi_unwinder)
9494 ftype = build_function_type_list (void_type_node, NULL_TREE);
9495 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9496 BUILT_IN_CXA_END_CLEANUP,
9497 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9500 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9501 local_define_builtin ("__builtin_unwind_resume", ftype,
9502 BUILT_IN_UNWIND_RESUME,
9503 ((targetm.except_unwind_info (&global_options)
9504 == UI_SJLJ)
9505 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9506 ECF_NORETURN);
9508 /* The exception object and filter values from the runtime. The argument
9509 must be zero before exception lowering, i.e. from the front end. After
9510 exception lowering, it will be the region number for the exception
9511 landing pad. These functions are PURE instead of CONST to prevent
9512 them from being hoisted past the exception edge that will initialize
9513 its value in the landing pad. */
9514 ftype = build_function_type_list (ptr_type_node,
9515 integer_type_node, NULL_TREE);
9516 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9517 "__builtin_eh_pointer", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9519 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9520 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9521 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9522 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9524 ftype = build_function_type_list (void_type_node,
9525 integer_type_node, integer_type_node,
9526 NULL_TREE);
9527 local_define_builtin ("__builtin_eh_copy_values", ftype,
9528 BUILT_IN_EH_COPY_VALUES,
9529 "__builtin_eh_copy_values", ECF_NOTHROW);
9531 /* Complex multiplication and division. These are handled as builtins
9532 rather than optabs because emit_library_call_value doesn't support
9533 complex. Further, we can do slightly better with folding these
9534 beasties if the real and complex parts of the arguments are separate. */
9536 int mode;
9538 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9540 char mode_name_buf[4], *q;
9541 const char *p;
9542 enum built_in_function mcode, dcode;
9543 tree type, inner_type;
9544 const char *prefix = "__";
9546 if (targetm.libfunc_gnu_prefix)
9547 prefix = "__gnu_";
9549 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9550 if (type == NULL)
9551 continue;
9552 inner_type = TREE_TYPE (type);
9554 ftype = build_function_type_list (type, inner_type, inner_type,
9555 inner_type, inner_type, NULL_TREE);
9557 mcode = ((enum built_in_function)
9558 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9559 dcode = ((enum built_in_function)
9560 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9562 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9563 *q = TOLOWER (*p);
9564 *q = '\0';
9566 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9567 NULL);
9568 local_define_builtin (built_in_names[mcode], ftype, mcode,
9569 built_in_names[mcode],
9570 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9572 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9573 NULL);
9574 local_define_builtin (built_in_names[dcode], ftype, dcode,
9575 built_in_names[dcode],
9576 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9581 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9582 better way.
9584 If we requested a pointer to a vector, build up the pointers that
9585 we stripped off while looking for the inner type. Similarly for
9586 return values from functions.
9588 The argument TYPE is the top of the chain, and BOTTOM is the
9589 new type which we will point to. */
9591 tree
9592 reconstruct_complex_type (tree type, tree bottom)
9594 tree inner, outer;
9596 if (TREE_CODE (type) == POINTER_TYPE)
9598 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9599 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9600 TYPE_REF_CAN_ALIAS_ALL (type));
9602 else if (TREE_CODE (type) == REFERENCE_TYPE)
9604 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9605 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9606 TYPE_REF_CAN_ALIAS_ALL (type));
9608 else if (TREE_CODE (type) == ARRAY_TYPE)
9610 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9611 outer = build_array_type (inner, TYPE_DOMAIN (type));
9613 else if (TREE_CODE (type) == FUNCTION_TYPE)
9615 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9616 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9618 else if (TREE_CODE (type) == METHOD_TYPE)
9620 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9621 /* The build_method_type_directly() routine prepends 'this' to argument list,
9622 so we must compensate by getting rid of it. */
9623 outer
9624 = build_method_type_directly
9625 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9626 inner,
9627 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9629 else if (TREE_CODE (type) == OFFSET_TYPE)
9631 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9632 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9634 else
9635 return bottom;
9637 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9638 TYPE_QUALS (type));
9641 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9642 the inner type. */
9643 tree
9644 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9646 int nunits;
9648 switch (GET_MODE_CLASS (mode))
9650 case MODE_VECTOR_INT:
9651 case MODE_VECTOR_FLOAT:
9652 case MODE_VECTOR_FRACT:
9653 case MODE_VECTOR_UFRACT:
9654 case MODE_VECTOR_ACCUM:
9655 case MODE_VECTOR_UACCUM:
9656 nunits = GET_MODE_NUNITS (mode);
9657 break;
9659 case MODE_INT:
9660 /* Check that there are no leftover bits. */
9661 gcc_assert (GET_MODE_BITSIZE (mode)
9662 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9664 nunits = GET_MODE_BITSIZE (mode)
9665 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9666 break;
9668 default:
9669 gcc_unreachable ();
9672 return make_vector_type (innertype, nunits, mode);
9675 /* Similarly, but takes the inner type and number of units, which must be
9676 a power of two. */
9678 tree
9679 build_vector_type (tree innertype, int nunits)
9681 return make_vector_type (innertype, nunits, VOIDmode);
9684 /* Similarly, but takes the inner type and number of units, which must be
9685 a power of two. */
9687 tree
9688 build_opaque_vector_type (tree innertype, int nunits)
9690 tree t;
9691 innertype = build_distinct_type_copy (innertype);
9692 t = make_vector_type (innertype, nunits, VOIDmode);
9693 TYPE_VECTOR_OPAQUE (t) = true;
9694 return t;
9698 /* Given an initializer INIT, return TRUE if INIT is zero or some
9699 aggregate of zeros. Otherwise return FALSE. */
9700 bool
9701 initializer_zerop (const_tree init)
9703 tree elt;
9705 STRIP_NOPS (init);
9707 switch (TREE_CODE (init))
9709 case INTEGER_CST:
9710 return integer_zerop (init);
9712 case REAL_CST:
9713 /* ??? Note that this is not correct for C4X float formats. There,
9714 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9715 negative exponent. */
9716 return real_zerop (init)
9717 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9719 case FIXED_CST:
9720 return fixed_zerop (init);
9722 case COMPLEX_CST:
9723 return integer_zerop (init)
9724 || (real_zerop (init)
9725 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9726 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9728 case VECTOR_CST:
9729 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
9730 if (!initializer_zerop (TREE_VALUE (elt)))
9731 return false;
9732 return true;
9734 case CONSTRUCTOR:
9736 unsigned HOST_WIDE_INT idx;
9738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
9739 if (!initializer_zerop (elt))
9740 return false;
9741 return true;
9744 case STRING_CST:
9746 int i;
9748 /* We need to loop through all elements to handle cases like
9749 "\0" and "\0foobar". */
9750 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
9751 if (TREE_STRING_POINTER (init)[i] != '\0')
9752 return false;
9754 return true;
9757 default:
9758 return false;
9762 /* Build an empty statement at location LOC. */
9764 tree
9765 build_empty_stmt (location_t loc)
9767 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
9768 SET_EXPR_LOCATION (t, loc);
9769 return t;
9773 /* Build an OpenMP clause with code CODE. LOC is the location of the
9774 clause. */
9776 tree
9777 build_omp_clause (location_t loc, enum omp_clause_code code)
9779 tree t;
9780 int size, length;
9782 length = omp_clause_num_ops[code];
9783 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
9785 record_node_allocation_statistics (OMP_CLAUSE, size);
9787 t = ggc_alloc_tree_node (size);
9788 memset (t, 0, size);
9789 TREE_SET_CODE (t, OMP_CLAUSE);
9790 OMP_CLAUSE_SET_CODE (t, code);
9791 OMP_CLAUSE_LOCATION (t) = loc;
9793 return t;
9796 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
9797 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
9798 Except for the CODE and operand count field, other storage for the
9799 object is initialized to zeros. */
9801 tree
9802 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
9804 tree t;
9805 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
9807 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
9808 gcc_assert (len >= 1);
9810 record_node_allocation_statistics (code, length);
9812 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
9814 TREE_SET_CODE (t, code);
9816 /* Can't use TREE_OPERAND to store the length because if checking is
9817 enabled, it will try to check the length before we store it. :-P */
9818 t->exp.operands[0] = build_int_cst (sizetype, len);
9820 return t;
9823 /* Helper function for build_call_* functions; build a CALL_EXPR with
9824 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
9825 the argument slots. */
9827 static tree
9828 build_call_1 (tree return_type, tree fn, int nargs)
9830 tree t;
9832 t = build_vl_exp (CALL_EXPR, nargs + 3);
9833 TREE_TYPE (t) = return_type;
9834 CALL_EXPR_FN (t) = fn;
9835 CALL_EXPR_STATIC_CHAIN (t) = NULL;
9837 return t;
9840 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9841 FN and a null static chain slot. NARGS is the number of call arguments
9842 which are specified as "..." arguments. */
9844 tree
9845 build_call_nary (tree return_type, tree fn, int nargs, ...)
9847 tree ret;
9848 va_list args;
9849 va_start (args, nargs);
9850 ret = build_call_valist (return_type, fn, nargs, args);
9851 va_end (args);
9852 return ret;
9855 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9856 FN and a null static chain slot. NARGS is the number of call arguments
9857 which are specified as a va_list ARGS. */
9859 tree
9860 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
9862 tree t;
9863 int i;
9865 t = build_call_1 (return_type, fn, nargs);
9866 for (i = 0; i < nargs; i++)
9867 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
9868 process_call_operands (t);
9869 return t;
9872 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9873 FN and a null static chain slot. NARGS is the number of call arguments
9874 which are specified as a tree array ARGS. */
9876 tree
9877 build_call_array_loc (location_t loc, tree return_type, tree fn,
9878 int nargs, const tree *args)
9880 tree t;
9881 int i;
9883 t = build_call_1 (return_type, fn, nargs);
9884 for (i = 0; i < nargs; i++)
9885 CALL_EXPR_ARG (t, i) = args[i];
9886 process_call_operands (t);
9887 SET_EXPR_LOCATION (t, loc);
9888 return t;
9891 /* Like build_call_array, but takes a VEC. */
9893 tree
9894 build_call_vec (tree return_type, tree fn, VEC(tree,gc) *args)
9896 tree ret, t;
9897 unsigned int ix;
9899 ret = build_call_1 (return_type, fn, VEC_length (tree, args));
9900 FOR_EACH_VEC_ELT (tree, args, ix, t)
9901 CALL_EXPR_ARG (ret, ix) = t;
9902 process_call_operands (ret);
9903 return ret;
9907 /* Returns true if it is possible to prove that the index of
9908 an array access REF (an ARRAY_REF expression) falls into the
9909 array bounds. */
9911 bool
9912 in_array_bounds_p (tree ref)
9914 tree idx = TREE_OPERAND (ref, 1);
9915 tree min, max;
9917 if (TREE_CODE (idx) != INTEGER_CST)
9918 return false;
9920 min = array_ref_low_bound (ref);
9921 max = array_ref_up_bound (ref);
9922 if (!min
9923 || !max
9924 || TREE_CODE (min) != INTEGER_CST
9925 || TREE_CODE (max) != INTEGER_CST)
9926 return false;
9928 if (tree_int_cst_lt (idx, min)
9929 || tree_int_cst_lt (max, idx))
9930 return false;
9932 return true;
9935 /* Returns true if it is possible to prove that the range of
9936 an array access REF (an ARRAY_RANGE_REF expression) falls
9937 into the array bounds. */
9939 bool
9940 range_in_array_bounds_p (tree ref)
9942 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
9943 tree range_min, range_max, min, max;
9945 range_min = TYPE_MIN_VALUE (domain_type);
9946 range_max = TYPE_MAX_VALUE (domain_type);
9947 if (!range_min
9948 || !range_max
9949 || TREE_CODE (range_min) != INTEGER_CST
9950 || TREE_CODE (range_max) != INTEGER_CST)
9951 return false;
9953 min = array_ref_low_bound (ref);
9954 max = array_ref_up_bound (ref);
9955 if (!min
9956 || !max
9957 || TREE_CODE (min) != INTEGER_CST
9958 || TREE_CODE (max) != INTEGER_CST)
9959 return false;
9961 if (tree_int_cst_lt (range_min, min)
9962 || tree_int_cst_lt (max, range_max))
9963 return false;
9965 return true;
9968 /* Return true if T (assumed to be a DECL) must be assigned a memory
9969 location. */
9971 bool
9972 needs_to_live_in_memory (const_tree t)
9974 if (TREE_CODE (t) == SSA_NAME)
9975 t = SSA_NAME_VAR (t);
9977 return (TREE_ADDRESSABLE (t)
9978 || is_global_var (t)
9979 || (TREE_CODE (t) == RESULT_DECL
9980 && !DECL_BY_REFERENCE (t)
9981 && aggregate_value_p (t, current_function_decl)));
9984 /* Return value of a constant X and sign-extend it. */
9986 HOST_WIDE_INT
9987 int_cst_value (const_tree x)
9989 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
9990 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
9992 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
9993 gcc_assert (TREE_INT_CST_HIGH (x) == 0
9994 || TREE_INT_CST_HIGH (x) == -1);
9996 if (bits < HOST_BITS_PER_WIDE_INT)
9998 bool negative = ((val >> (bits - 1)) & 1) != 0;
9999 if (negative)
10000 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10001 else
10002 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10005 return val;
10008 /* Return value of a constant X and sign-extend it. */
10010 HOST_WIDEST_INT
10011 widest_int_cst_value (const_tree x)
10013 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10014 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10016 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10017 gcc_assert (HOST_BITS_PER_WIDEST_INT >= 2 * HOST_BITS_PER_WIDE_INT);
10018 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10019 << HOST_BITS_PER_WIDE_INT);
10020 #else
10021 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10022 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10023 || TREE_INT_CST_HIGH (x) == -1);
10024 #endif
10026 if (bits < HOST_BITS_PER_WIDEST_INT)
10028 bool negative = ((val >> (bits - 1)) & 1) != 0;
10029 if (negative)
10030 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10031 else
10032 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10035 return val;
10038 /* If TYPE is an integral type, return an equivalent type which is
10039 unsigned iff UNSIGNEDP is true. If TYPE is not an integral type,
10040 return TYPE itself. */
10042 tree
10043 signed_or_unsigned_type_for (int unsignedp, tree type)
10045 tree t = type;
10046 if (POINTER_TYPE_P (type))
10048 /* If the pointer points to the normal address space, use the
10049 size_type_node. Otherwise use an appropriate size for the pointer
10050 based on the named address space it points to. */
10051 if (!TYPE_ADDR_SPACE (TREE_TYPE (t)))
10052 t = size_type_node;
10053 else
10054 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
10057 if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
10058 return t;
10060 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
10063 /* Returns unsigned variant of TYPE. */
10065 tree
10066 unsigned_type_for (tree type)
10068 return signed_or_unsigned_type_for (1, type);
10071 /* Returns signed variant of TYPE. */
10073 tree
10074 signed_type_for (tree type)
10076 return signed_or_unsigned_type_for (0, type);
10079 /* Returns the largest value obtainable by casting something in INNER type to
10080 OUTER type. */
10082 tree
10083 upper_bound_in_type (tree outer, tree inner)
10085 double_int high;
10086 unsigned int det = 0;
10087 unsigned oprec = TYPE_PRECISION (outer);
10088 unsigned iprec = TYPE_PRECISION (inner);
10089 unsigned prec;
10091 /* Compute a unique number for every combination. */
10092 det |= (oprec > iprec) ? 4 : 0;
10093 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10094 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10096 /* Determine the exponent to use. */
10097 switch (det)
10099 case 0:
10100 case 1:
10101 /* oprec <= iprec, outer: signed, inner: don't care. */
10102 prec = oprec - 1;
10103 break;
10104 case 2:
10105 case 3:
10106 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10107 prec = oprec;
10108 break;
10109 case 4:
10110 /* oprec > iprec, outer: signed, inner: signed. */
10111 prec = iprec - 1;
10112 break;
10113 case 5:
10114 /* oprec > iprec, outer: signed, inner: unsigned. */
10115 prec = iprec;
10116 break;
10117 case 6:
10118 /* oprec > iprec, outer: unsigned, inner: signed. */
10119 prec = oprec;
10120 break;
10121 case 7:
10122 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10123 prec = iprec;
10124 break;
10125 default:
10126 gcc_unreachable ();
10129 /* Compute 2^^prec - 1. */
10130 if (prec <= HOST_BITS_PER_WIDE_INT)
10132 high.high = 0;
10133 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10134 >> (HOST_BITS_PER_WIDE_INT - prec));
10136 else
10138 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10139 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
10140 high.low = ~(unsigned HOST_WIDE_INT) 0;
10143 return double_int_to_tree (outer, high);
10146 /* Returns the smallest value obtainable by casting something in INNER type to
10147 OUTER type. */
10149 tree
10150 lower_bound_in_type (tree outer, tree inner)
10152 double_int low;
10153 unsigned oprec = TYPE_PRECISION (outer);
10154 unsigned iprec = TYPE_PRECISION (inner);
10156 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10157 and obtain 0. */
10158 if (TYPE_UNSIGNED (outer)
10159 /* If we are widening something of an unsigned type, OUTER type
10160 contains all values of INNER type. In particular, both INNER
10161 and OUTER types have zero in common. */
10162 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10163 low.low = low.high = 0;
10164 else
10166 /* If we are widening a signed type to another signed type, we
10167 want to obtain -2^^(iprec-1). If we are keeping the
10168 precision or narrowing to a signed type, we want to obtain
10169 -2^(oprec-1). */
10170 unsigned prec = oprec > iprec ? iprec : oprec;
10172 if (prec <= HOST_BITS_PER_WIDE_INT)
10174 low.high = ~(unsigned HOST_WIDE_INT) 0;
10175 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10177 else
10179 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10180 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10181 low.low = 0;
10185 return double_int_to_tree (outer, low);
10188 /* Return nonzero if two operands that are suitable for PHI nodes are
10189 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10190 SSA_NAME or invariant. Note that this is strictly an optimization.
10191 That is, callers of this function can directly call operand_equal_p
10192 and get the same result, only slower. */
10195 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10197 if (arg0 == arg1)
10198 return 1;
10199 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10200 return 0;
10201 return operand_equal_p (arg0, arg1, 0);
10204 /* Returns number of zeros at the end of binary representation of X.
10206 ??? Use ffs if available? */
10208 tree
10209 num_ending_zeros (const_tree x)
10211 unsigned HOST_WIDE_INT fr, nfr;
10212 unsigned num, abits;
10213 tree type = TREE_TYPE (x);
10215 if (TREE_INT_CST_LOW (x) == 0)
10217 num = HOST_BITS_PER_WIDE_INT;
10218 fr = TREE_INT_CST_HIGH (x);
10220 else
10222 num = 0;
10223 fr = TREE_INT_CST_LOW (x);
10226 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10228 nfr = fr >> abits;
10229 if (nfr << abits == fr)
10231 num += abits;
10232 fr = nfr;
10236 if (num > TYPE_PRECISION (type))
10237 num = TYPE_PRECISION (type);
10239 return build_int_cst_type (type, num);
10243 #define WALK_SUBTREE(NODE) \
10244 do \
10246 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10247 if (result) \
10248 return result; \
10250 while (0)
10252 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10253 be walked whenever a type is seen in the tree. Rest of operands and return
10254 value are as for walk_tree. */
10256 static tree
10257 walk_type_fields (tree type, walk_tree_fn func, void *data,
10258 struct pointer_set_t *pset, walk_tree_lh lh)
10260 tree result = NULL_TREE;
10262 switch (TREE_CODE (type))
10264 case POINTER_TYPE:
10265 case REFERENCE_TYPE:
10266 /* We have to worry about mutually recursive pointers. These can't
10267 be written in C. They can in Ada. It's pathological, but
10268 there's an ACATS test (c38102a) that checks it. Deal with this
10269 by checking if we're pointing to another pointer, that one
10270 points to another pointer, that one does too, and we have no htab.
10271 If so, get a hash table. We check three levels deep to avoid
10272 the cost of the hash table if we don't need one. */
10273 if (POINTER_TYPE_P (TREE_TYPE (type))
10274 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10275 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10276 && !pset)
10278 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10279 func, data);
10280 if (result)
10281 return result;
10283 break;
10286 /* ... fall through ... */
10288 case COMPLEX_TYPE:
10289 WALK_SUBTREE (TREE_TYPE (type));
10290 break;
10292 case METHOD_TYPE:
10293 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10295 /* Fall through. */
10297 case FUNCTION_TYPE:
10298 WALK_SUBTREE (TREE_TYPE (type));
10300 tree arg;
10302 /* We never want to walk into default arguments. */
10303 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10304 WALK_SUBTREE (TREE_VALUE (arg));
10306 break;
10308 case ARRAY_TYPE:
10309 /* Don't follow this nodes's type if a pointer for fear that
10310 we'll have infinite recursion. If we have a PSET, then we
10311 need not fear. */
10312 if (pset
10313 || (!POINTER_TYPE_P (TREE_TYPE (type))
10314 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10315 WALK_SUBTREE (TREE_TYPE (type));
10316 WALK_SUBTREE (TYPE_DOMAIN (type));
10317 break;
10319 case OFFSET_TYPE:
10320 WALK_SUBTREE (TREE_TYPE (type));
10321 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10322 break;
10324 default:
10325 break;
10328 return NULL_TREE;
10331 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10332 called with the DATA and the address of each sub-tree. If FUNC returns a
10333 non-NULL value, the traversal is stopped, and the value returned by FUNC
10334 is returned. If PSET is non-NULL it is used to record the nodes visited,
10335 and to avoid visiting a node more than once. */
10337 tree
10338 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10339 struct pointer_set_t *pset, walk_tree_lh lh)
10341 enum tree_code code;
10342 int walk_subtrees;
10343 tree result;
10345 #define WALK_SUBTREE_TAIL(NODE) \
10346 do \
10348 tp = & (NODE); \
10349 goto tail_recurse; \
10351 while (0)
10353 tail_recurse:
10354 /* Skip empty subtrees. */
10355 if (!*tp)
10356 return NULL_TREE;
10358 /* Don't walk the same tree twice, if the user has requested
10359 that we avoid doing so. */
10360 if (pset && pointer_set_insert (pset, *tp))
10361 return NULL_TREE;
10363 /* Call the function. */
10364 walk_subtrees = 1;
10365 result = (*func) (tp, &walk_subtrees, data);
10367 /* If we found something, return it. */
10368 if (result)
10369 return result;
10371 code = TREE_CODE (*tp);
10373 /* Even if we didn't, FUNC may have decided that there was nothing
10374 interesting below this point in the tree. */
10375 if (!walk_subtrees)
10377 /* But we still need to check our siblings. */
10378 if (code == TREE_LIST)
10379 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10380 else if (code == OMP_CLAUSE)
10381 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10382 else
10383 return NULL_TREE;
10386 if (lh)
10388 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10389 if (result || !walk_subtrees)
10390 return result;
10393 switch (code)
10395 case ERROR_MARK:
10396 case IDENTIFIER_NODE:
10397 case INTEGER_CST:
10398 case REAL_CST:
10399 case FIXED_CST:
10400 case VECTOR_CST:
10401 case STRING_CST:
10402 case BLOCK:
10403 case PLACEHOLDER_EXPR:
10404 case SSA_NAME:
10405 case FIELD_DECL:
10406 case RESULT_DECL:
10407 /* None of these have subtrees other than those already walked
10408 above. */
10409 break;
10411 case TREE_LIST:
10412 WALK_SUBTREE (TREE_VALUE (*tp));
10413 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10414 break;
10416 case TREE_VEC:
10418 int len = TREE_VEC_LENGTH (*tp);
10420 if (len == 0)
10421 break;
10423 /* Walk all elements but the first. */
10424 while (--len)
10425 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10427 /* Now walk the first one as a tail call. */
10428 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10431 case COMPLEX_CST:
10432 WALK_SUBTREE (TREE_REALPART (*tp));
10433 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10435 case CONSTRUCTOR:
10437 unsigned HOST_WIDE_INT idx;
10438 constructor_elt *ce;
10440 for (idx = 0;
10441 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
10442 idx++)
10443 WALK_SUBTREE (ce->value);
10445 break;
10447 case SAVE_EXPR:
10448 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10450 case BIND_EXPR:
10452 tree decl;
10453 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10455 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10456 into declarations that are just mentioned, rather than
10457 declared; they don't really belong to this part of the tree.
10458 And, we can see cycles: the initializer for a declaration
10459 can refer to the declaration itself. */
10460 WALK_SUBTREE (DECL_INITIAL (decl));
10461 WALK_SUBTREE (DECL_SIZE (decl));
10462 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10464 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10467 case STATEMENT_LIST:
10469 tree_stmt_iterator i;
10470 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10471 WALK_SUBTREE (*tsi_stmt_ptr (i));
10473 break;
10475 case OMP_CLAUSE:
10476 switch (OMP_CLAUSE_CODE (*tp))
10478 case OMP_CLAUSE_PRIVATE:
10479 case OMP_CLAUSE_SHARED:
10480 case OMP_CLAUSE_FIRSTPRIVATE:
10481 case OMP_CLAUSE_COPYIN:
10482 case OMP_CLAUSE_COPYPRIVATE:
10483 case OMP_CLAUSE_IF:
10484 case OMP_CLAUSE_NUM_THREADS:
10485 case OMP_CLAUSE_SCHEDULE:
10486 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10487 /* FALLTHRU */
10489 case OMP_CLAUSE_NOWAIT:
10490 case OMP_CLAUSE_ORDERED:
10491 case OMP_CLAUSE_DEFAULT:
10492 case OMP_CLAUSE_UNTIED:
10493 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10495 case OMP_CLAUSE_LASTPRIVATE:
10496 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10497 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10498 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10500 case OMP_CLAUSE_COLLAPSE:
10502 int i;
10503 for (i = 0; i < 3; i++)
10504 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10505 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10508 case OMP_CLAUSE_REDUCTION:
10510 int i;
10511 for (i = 0; i < 4; i++)
10512 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10513 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10516 default:
10517 gcc_unreachable ();
10519 break;
10521 case TARGET_EXPR:
10523 int i, len;
10525 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10526 But, we only want to walk once. */
10527 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10528 for (i = 0; i < len; ++i)
10529 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10530 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10533 case DECL_EXPR:
10534 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10535 defining. We only want to walk into these fields of a type in this
10536 case and not in the general case of a mere reference to the type.
10538 The criterion is as follows: if the field can be an expression, it
10539 must be walked only here. This should be in keeping with the fields
10540 that are directly gimplified in gimplify_type_sizes in order for the
10541 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10542 variable-sized types.
10544 Note that DECLs get walked as part of processing the BIND_EXPR. */
10545 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10547 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10548 if (TREE_CODE (*type_p) == ERROR_MARK)
10549 return NULL_TREE;
10551 /* Call the function for the type. See if it returns anything or
10552 doesn't want us to continue. If we are to continue, walk both
10553 the normal fields and those for the declaration case. */
10554 result = (*func) (type_p, &walk_subtrees, data);
10555 if (result || !walk_subtrees)
10556 return result;
10558 result = walk_type_fields (*type_p, func, data, pset, lh);
10559 if (result)
10560 return result;
10562 /* If this is a record type, also walk the fields. */
10563 if (RECORD_OR_UNION_TYPE_P (*type_p))
10565 tree field;
10567 for (field = TYPE_FIELDS (*type_p); field;
10568 field = DECL_CHAIN (field))
10570 /* We'd like to look at the type of the field, but we can
10571 easily get infinite recursion. So assume it's pointed
10572 to elsewhere in the tree. Also, ignore things that
10573 aren't fields. */
10574 if (TREE_CODE (field) != FIELD_DECL)
10575 continue;
10577 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10578 WALK_SUBTREE (DECL_SIZE (field));
10579 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10580 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10581 WALK_SUBTREE (DECL_QUALIFIER (field));
10585 /* Same for scalar types. */
10586 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10587 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10588 || TREE_CODE (*type_p) == INTEGER_TYPE
10589 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10590 || TREE_CODE (*type_p) == REAL_TYPE)
10592 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10593 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10596 WALK_SUBTREE (TYPE_SIZE (*type_p));
10597 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10599 /* FALLTHRU */
10601 default:
10602 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10604 int i, len;
10606 /* Walk over all the sub-trees of this operand. */
10607 len = TREE_OPERAND_LENGTH (*tp);
10609 /* Go through the subtrees. We need to do this in forward order so
10610 that the scope of a FOR_EXPR is handled properly. */
10611 if (len)
10613 for (i = 0; i < len - 1; ++i)
10614 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10615 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10618 /* If this is a type, walk the needed fields in the type. */
10619 else if (TYPE_P (*tp))
10620 return walk_type_fields (*tp, func, data, pset, lh);
10621 break;
10624 /* We didn't find what we were looking for. */
10625 return NULL_TREE;
10627 #undef WALK_SUBTREE_TAIL
10629 #undef WALK_SUBTREE
10631 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10633 tree
10634 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10635 walk_tree_lh lh)
10637 tree result;
10638 struct pointer_set_t *pset;
10640 pset = pointer_set_create ();
10641 result = walk_tree_1 (tp, func, data, pset, lh);
10642 pointer_set_destroy (pset);
10643 return result;
10647 tree *
10648 tree_block (tree t)
10650 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10652 if (IS_EXPR_CODE_CLASS (c))
10653 return &t->exp.block;
10654 gcc_unreachable ();
10655 return NULL;
10658 /* Create a nameless artificial label and put it in the current
10659 function context. The label has a location of LOC. Returns the
10660 newly created label. */
10662 tree
10663 create_artificial_label (location_t loc)
10665 tree lab = build_decl (loc,
10666 LABEL_DECL, NULL_TREE, void_type_node);
10668 DECL_ARTIFICIAL (lab) = 1;
10669 DECL_IGNORED_P (lab) = 1;
10670 DECL_CONTEXT (lab) = current_function_decl;
10671 return lab;
10674 /* Given a tree, try to return a useful variable name that we can use
10675 to prefix a temporary that is being assigned the value of the tree.
10676 I.E. given <temp> = &A, return A. */
10678 const char *
10679 get_name (tree t)
10681 tree stripped_decl;
10683 stripped_decl = t;
10684 STRIP_NOPS (stripped_decl);
10685 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10686 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10687 else
10689 switch (TREE_CODE (stripped_decl))
10691 case ADDR_EXPR:
10692 return get_name (TREE_OPERAND (stripped_decl, 0));
10693 default:
10694 return NULL;
10699 /* Return true if TYPE has a variable argument list. */
10701 bool
10702 stdarg_p (const_tree fntype)
10704 function_args_iterator args_iter;
10705 tree n = NULL_TREE, t;
10707 if (!fntype)
10708 return false;
10710 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
10712 n = t;
10715 return n != NULL_TREE && n != void_type_node;
10718 /* Return true if TYPE has a prototype. */
10720 bool
10721 prototype_p (tree fntype)
10723 tree t;
10725 gcc_assert (fntype != NULL_TREE);
10727 t = TYPE_ARG_TYPES (fntype);
10728 return (t != NULL_TREE);
10731 /* If BLOCK is inlined from an __attribute__((__artificial__))
10732 routine, return pointer to location from where it has been
10733 called. */
10734 location_t *
10735 block_nonartificial_location (tree block)
10737 location_t *ret = NULL;
10739 while (block && TREE_CODE (block) == BLOCK
10740 && BLOCK_ABSTRACT_ORIGIN (block))
10742 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
10744 while (TREE_CODE (ao) == BLOCK
10745 && BLOCK_ABSTRACT_ORIGIN (ao)
10746 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
10747 ao = BLOCK_ABSTRACT_ORIGIN (ao);
10749 if (TREE_CODE (ao) == FUNCTION_DECL)
10751 /* If AO is an artificial inline, point RET to the
10752 call site locus at which it has been inlined and continue
10753 the loop, in case AO's caller is also an artificial
10754 inline. */
10755 if (DECL_DECLARED_INLINE_P (ao)
10756 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
10757 ret = &BLOCK_SOURCE_LOCATION (block);
10758 else
10759 break;
10761 else if (TREE_CODE (ao) != BLOCK)
10762 break;
10764 block = BLOCK_SUPERCONTEXT (block);
10766 return ret;
10770 /* If EXP is inlined from an __attribute__((__artificial__))
10771 function, return the location of the original call expression. */
10773 location_t
10774 tree_nonartificial_location (tree exp)
10776 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
10778 if (loc)
10779 return *loc;
10780 else
10781 return EXPR_LOCATION (exp);
10785 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
10786 nodes. */
10788 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
10790 static hashval_t
10791 cl_option_hash_hash (const void *x)
10793 const_tree const t = (const_tree) x;
10794 const char *p;
10795 size_t i;
10796 size_t len = 0;
10797 hashval_t hash = 0;
10799 if (TREE_CODE (t) == OPTIMIZATION_NODE)
10801 p = (const char *)TREE_OPTIMIZATION (t);
10802 len = sizeof (struct cl_optimization);
10805 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
10807 p = (const char *)TREE_TARGET_OPTION (t);
10808 len = sizeof (struct cl_target_option);
10811 else
10812 gcc_unreachable ();
10814 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
10815 something else. */
10816 for (i = 0; i < len; i++)
10817 if (p[i])
10818 hash = (hash << 4) ^ ((i << 2) | p[i]);
10820 return hash;
10823 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
10824 TARGET_OPTION tree node) is the same as that given by *Y, which is the
10825 same. */
10827 static int
10828 cl_option_hash_eq (const void *x, const void *y)
10830 const_tree const xt = (const_tree) x;
10831 const_tree const yt = (const_tree) y;
10832 const char *xp;
10833 const char *yp;
10834 size_t len;
10836 if (TREE_CODE (xt) != TREE_CODE (yt))
10837 return 0;
10839 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
10841 xp = (const char *)TREE_OPTIMIZATION (xt);
10842 yp = (const char *)TREE_OPTIMIZATION (yt);
10843 len = sizeof (struct cl_optimization);
10846 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
10848 xp = (const char *)TREE_TARGET_OPTION (xt);
10849 yp = (const char *)TREE_TARGET_OPTION (yt);
10850 len = sizeof (struct cl_target_option);
10853 else
10854 gcc_unreachable ();
10856 return (memcmp (xp, yp, len) == 0);
10859 /* Build an OPTIMIZATION_NODE based on the current options. */
10861 tree
10862 build_optimization_node (void)
10864 tree t;
10865 void **slot;
10867 /* Use the cache of optimization nodes. */
10869 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
10870 &global_options);
10872 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
10873 t = (tree) *slot;
10874 if (!t)
10876 /* Insert this one into the hash table. */
10877 t = cl_optimization_node;
10878 *slot = t;
10880 /* Make a new node for next time round. */
10881 cl_optimization_node = make_node (OPTIMIZATION_NODE);
10884 return t;
10887 /* Build a TARGET_OPTION_NODE based on the current options. */
10889 tree
10890 build_target_option_node (void)
10892 tree t;
10893 void **slot;
10895 /* Use the cache of optimization nodes. */
10897 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
10898 &global_options);
10900 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
10901 t = (tree) *slot;
10902 if (!t)
10904 /* Insert this one into the hash table. */
10905 t = cl_target_option_node;
10906 *slot = t;
10908 /* Make a new node for next time round. */
10909 cl_target_option_node = make_node (TARGET_OPTION_NODE);
10912 return t;
10915 /* Determine the "ultimate origin" of a block. The block may be an inlined
10916 instance of an inlined instance of a block which is local to an inline
10917 function, so we have to trace all of the way back through the origin chain
10918 to find out what sort of node actually served as the original seed for the
10919 given block. */
10921 tree
10922 block_ultimate_origin (const_tree block)
10924 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
10926 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
10927 nodes in the function to point to themselves; ignore that if
10928 we're trying to output the abstract instance of this function. */
10929 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
10930 return NULL_TREE;
10932 if (immediate_origin == NULL_TREE)
10933 return NULL_TREE;
10934 else
10936 tree ret_val;
10937 tree lookahead = immediate_origin;
10941 ret_val = lookahead;
10942 lookahead = (TREE_CODE (ret_val) == BLOCK
10943 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
10945 while (lookahead != NULL && lookahead != ret_val);
10947 /* The block's abstract origin chain may not be the *ultimate* origin of
10948 the block. It could lead to a DECL that has an abstract origin set.
10949 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
10950 will give us if it has one). Note that DECL's abstract origins are
10951 supposed to be the most distant ancestor (or so decl_ultimate_origin
10952 claims), so we don't need to loop following the DECL origins. */
10953 if (DECL_P (ret_val))
10954 return DECL_ORIGIN (ret_val);
10956 return ret_val;
10960 /* Return true if T1 and T2 are equivalent lists. */
10962 bool
10963 list_equal_p (const_tree t1, const_tree t2)
10965 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
10966 if (TREE_VALUE (t1) != TREE_VALUE (t2))
10967 return false;
10968 return !t1 && !t2;
10971 /* Return true iff conversion in EXP generates no instruction. Mark
10972 it inline so that we fully inline into the stripping functions even
10973 though we have two uses of this function. */
10975 static inline bool
10976 tree_nop_conversion (const_tree exp)
10978 tree outer_type, inner_type;
10980 if (!CONVERT_EXPR_P (exp)
10981 && TREE_CODE (exp) != NON_LVALUE_EXPR)
10982 return false;
10983 if (TREE_OPERAND (exp, 0) == error_mark_node)
10984 return false;
10986 outer_type = TREE_TYPE (exp);
10987 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
10989 if (!inner_type)
10990 return false;
10992 /* Use precision rather then machine mode when we can, which gives
10993 the correct answer even for submode (bit-field) types. */
10994 if ((INTEGRAL_TYPE_P (outer_type)
10995 || POINTER_TYPE_P (outer_type)
10996 || TREE_CODE (outer_type) == OFFSET_TYPE)
10997 && (INTEGRAL_TYPE_P (inner_type)
10998 || POINTER_TYPE_P (inner_type)
10999 || TREE_CODE (inner_type) == OFFSET_TYPE))
11000 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11002 /* Otherwise fall back on comparing machine modes (e.g. for
11003 aggregate types, floats). */
11004 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11007 /* Return true iff conversion in EXP generates no instruction. Don't
11008 consider conversions changing the signedness. */
11010 static bool
11011 tree_sign_nop_conversion (const_tree exp)
11013 tree outer_type, inner_type;
11015 if (!tree_nop_conversion (exp))
11016 return false;
11018 outer_type = TREE_TYPE (exp);
11019 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11021 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11022 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11025 /* Strip conversions from EXP according to tree_nop_conversion and
11026 return the resulting expression. */
11028 tree
11029 tree_strip_nop_conversions (tree exp)
11031 while (tree_nop_conversion (exp))
11032 exp = TREE_OPERAND (exp, 0);
11033 return exp;
11036 /* Strip conversions from EXP according to tree_sign_nop_conversion
11037 and return the resulting expression. */
11039 tree
11040 tree_strip_sign_nop_conversions (tree exp)
11042 while (tree_sign_nop_conversion (exp))
11043 exp = TREE_OPERAND (exp, 0);
11044 return exp;
11047 static GTY(()) tree gcc_eh_personality_decl;
11049 /* Return the GCC personality function decl. */
11051 tree
11052 lhd_gcc_personality (void)
11054 if (!gcc_eh_personality_decl)
11055 gcc_eh_personality_decl = build_personality_function ("gcc");
11056 return gcc_eh_personality_decl;
11059 /* Try to find a base info of BINFO that would have its field decl at offset
11060 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11061 found, return, otherwise return NULL_TREE. */
11063 tree
11064 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11066 tree type = BINFO_TYPE (binfo);
11068 while (true)
11070 HOST_WIDE_INT pos, size;
11071 tree fld;
11072 int i;
11074 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11075 return binfo;
11076 if (offset < 0)
11077 return NULL_TREE;
11079 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11081 if (TREE_CODE (fld) != FIELD_DECL)
11082 continue;
11084 pos = int_bit_position (fld);
11085 size = tree_low_cst (DECL_SIZE (fld), 1);
11086 if (pos <= offset && (pos + size) > offset)
11087 break;
11089 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11090 return NULL_TREE;
11092 if (!DECL_ARTIFICIAL (fld))
11094 binfo = TYPE_BINFO (TREE_TYPE (fld));
11095 if (!binfo)
11096 return NULL_TREE;
11098 /* Offset 0 indicates the primary base, whose vtable contents are
11099 represented in the binfo for the derived class. */
11100 else if (offset != 0)
11102 tree base_binfo, found_binfo = NULL_TREE;
11103 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11104 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11106 found_binfo = base_binfo;
11107 break;
11109 if (!found_binfo)
11110 return NULL_TREE;
11111 binfo = found_binfo;
11114 type = TREE_TYPE (fld);
11115 offset -= pos;
11119 /* Returns true if X is a typedef decl. */
11121 bool
11122 is_typedef_decl (tree x)
11124 return (x && TREE_CODE (x) == TYPE_DECL
11125 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11128 /* Returns true iff TYPE is a type variant created for a typedef. */
11130 bool
11131 typedef_variant_p (tree type)
11133 return is_typedef_decl (TYPE_NAME (type));
11136 /* Warn about a use of an identifier which was marked deprecated. */
11137 void
11138 warn_deprecated_use (tree node, tree attr)
11140 const char *msg;
11142 if (node == 0 || !warn_deprecated_decl)
11143 return;
11145 if (!attr)
11147 if (DECL_P (node))
11148 attr = DECL_ATTRIBUTES (node);
11149 else if (TYPE_P (node))
11151 tree decl = TYPE_STUB_DECL (node);
11152 if (decl)
11153 attr = lookup_attribute ("deprecated",
11154 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11158 if (attr)
11159 attr = lookup_attribute ("deprecated", attr);
11161 if (attr)
11162 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11163 else
11164 msg = NULL;
11166 if (DECL_P (node))
11168 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11169 if (msg)
11170 warning (OPT_Wdeprecated_declarations,
11171 "%qD is deprecated (declared at %s:%d): %s",
11172 node, xloc.file, xloc.line, msg);
11173 else
11174 warning (OPT_Wdeprecated_declarations,
11175 "%qD is deprecated (declared at %s:%d)",
11176 node, xloc.file, xloc.line);
11178 else if (TYPE_P (node))
11180 tree what = NULL_TREE;
11181 tree decl = TYPE_STUB_DECL (node);
11183 if (TYPE_NAME (node))
11185 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11186 what = TYPE_NAME (node);
11187 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11188 && DECL_NAME (TYPE_NAME (node)))
11189 what = DECL_NAME (TYPE_NAME (node));
11192 if (decl)
11194 expanded_location xloc
11195 = expand_location (DECL_SOURCE_LOCATION (decl));
11196 if (what)
11198 if (msg)
11199 warning (OPT_Wdeprecated_declarations,
11200 "%qE is deprecated (declared at %s:%d): %s",
11201 what, xloc.file, xloc.line, msg);
11202 else
11203 warning (OPT_Wdeprecated_declarations,
11204 "%qE is deprecated (declared at %s:%d)", what,
11205 xloc.file, xloc.line);
11207 else
11209 if (msg)
11210 warning (OPT_Wdeprecated_declarations,
11211 "type is deprecated (declared at %s:%d): %s",
11212 xloc.file, xloc.line, msg);
11213 else
11214 warning (OPT_Wdeprecated_declarations,
11215 "type is deprecated (declared at %s:%d)",
11216 xloc.file, xloc.line);
11219 else
11221 if (what)
11223 if (msg)
11224 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11225 what, msg);
11226 else
11227 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11229 else
11231 if (msg)
11232 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11233 msg);
11234 else
11235 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11241 #include "gt-tree.h"