Backport r203445 from v17
[official-gcc.git] / gcc-4_8 / gcc / tree.c
blob7a36fa8b67035124c4e9e235c29f28e743eb158b
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "tm_p.h"
37 #include "function.h"
38 #include "obstack.h"
39 #include "toplev.h" /* get_random_seed */
40 #include "ggc.h"
41 #include "hashtab.h"
42 #include "filenames.h"
43 #include "output.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "tree-flow.h"
51 #include "params.h"
52 #include "pointer-set.h"
53 #include "tree-pass.h"
54 #include "langhooks-def.h"
55 #include "diagnostic.h"
56 #include "tree-diagnostic.h"
57 #include "tree-pretty-print.h"
58 #include "cgraph.h"
59 #include "except.h"
60 #include "debug.h"
61 #include "intl.h"
63 /* Tree code classes. */
65 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
66 #define END_OF_BASE_TREE_CODES tcc_exceptional,
68 const enum tree_code_class tree_code_type[] = {
69 #include "all-tree.def"
72 #undef DEFTREECODE
73 #undef END_OF_BASE_TREE_CODES
75 /* Table indexed by tree code giving number of expression
76 operands beyond the fixed part of the node structure.
77 Not used for types or decls. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
80 #define END_OF_BASE_TREE_CODES 0,
82 const unsigned char tree_code_length[] = {
83 #include "all-tree.def"
86 #undef DEFTREECODE
87 #undef END_OF_BASE_TREE_CODES
89 /* Names of tree components.
90 Used for printing out the tree and error messages. */
91 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
92 #define END_OF_BASE_TREE_CODES "@dummy",
94 const char *const tree_code_name[] = {
95 #include "all-tree.def"
98 #undef DEFTREECODE
99 #undef END_OF_BASE_TREE_CODES
101 /* Each tree code class has an associated string representation.
102 These must correspond to the tree_code_class entries. */
104 const char *const tree_code_class_strings[] =
106 "exceptional",
107 "constant",
108 "type",
109 "declaration",
110 "reference",
111 "comparison",
112 "unary",
113 "binary",
114 "statement",
115 "vl_exp",
116 "expression"
119 /* obstack.[ch] explicitly declined to prototype this. */
120 extern int _obstack_allocated_p (struct obstack *h, void *obj);
122 /* Statistics-gathering stuff. */
124 static int tree_code_counts[MAX_TREE_CODES];
125 int tree_node_counts[(int) all_kinds];
126 int tree_node_sizes[(int) all_kinds];
128 /* Keep in sync with tree.h:enum tree_node_kind. */
129 static const char * const tree_node_kind_names[] = {
130 "decls",
131 "types",
132 "blocks",
133 "stmts",
134 "refs",
135 "exprs",
136 "constants",
137 "identifiers",
138 "vecs",
139 "binfos",
140 "ssa names",
141 "constructors",
142 "random kinds",
143 "lang_decl kinds",
144 "lang_type kinds",
145 "omp clauses",
148 /* Unique id for next decl created. */
149 static GTY(()) int next_decl_uid;
150 /* Unique id for next type created. */
151 static GTY(()) int next_type_uid = 1;
152 /* Unique id for next debug decl created. Use negative numbers,
153 to catch erroneous uses. */
154 static GTY(()) int next_debug_decl_uid;
156 /* Since we cannot rehash a type after it is in the table, we have to
157 keep the hash code. */
159 struct GTY(()) type_hash {
160 unsigned long hash;
161 tree type;
164 /* Initial size of the hash table (rounded to next prime). */
165 #define TYPE_HASH_INITIAL_SIZE 1000
167 /* Now here is the hash table. When recording a type, it is added to
168 the slot whose index is the hash code. Note that the hash table is
169 used for several kinds of types (function types, array types and
170 array index range types, for now). While all these live in the
171 same table, they are completely independent, and the hash code is
172 computed differently for each of these. */
174 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
175 htab_t type_hash_table;
177 /* Hash table and temporary node for larger integer const values. */
178 static GTY (()) tree int_cst_node;
179 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
180 htab_t int_cst_hash_table;
182 /* Hash table for optimization flags and target option flags. Use the same
183 hash table for both sets of options. Nodes for building the current
184 optimization and target option nodes. The assumption is most of the time
185 the options created will already be in the hash table, so we avoid
186 allocating and freeing up a node repeatably. */
187 static GTY (()) tree cl_optimization_node;
188 static GTY (()) tree cl_target_option_node;
189 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
190 htab_t cl_option_hash_table;
192 /* General tree->tree mapping structure for use in hash tables. */
195 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
196 htab_t debug_expr_for_decl;
198 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
199 htab_t value_expr_for_decl;
201 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
202 htab_t debug_args_for_decl;
204 static GTY ((if_marked ("tree_priority_map_marked_p"),
205 param_is (struct tree_priority_map)))
206 htab_t init_priority_for_decl;
208 static void set_type_quals (tree, int);
209 static int type_hash_eq (const void *, const void *);
210 static hashval_t type_hash_hash (const void *);
211 static hashval_t int_cst_hash_hash (const void *);
212 static int int_cst_hash_eq (const void *, const void *);
213 static hashval_t cl_option_hash_hash (const void *);
214 static int cl_option_hash_eq (const void *, const void *);
215 static void print_type_hash_statistics (void);
216 static void print_debug_expr_statistics (void);
217 static void print_value_expr_statistics (void);
218 static int type_hash_marked_p (const void *);
219 static unsigned int type_hash_list (const_tree, hashval_t);
220 static unsigned int attribute_hash_list (const_tree, hashval_t);
222 tree global_trees[TI_MAX];
223 tree integer_types[itk_none];
225 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
227 /* Number of operands for each OpenMP clause. */
228 unsigned const char omp_clause_num_ops[] =
230 0, /* OMP_CLAUSE_ERROR */
231 1, /* OMP_CLAUSE_PRIVATE */
232 1, /* OMP_CLAUSE_SHARED */
233 1, /* OMP_CLAUSE_FIRSTPRIVATE */
234 2, /* OMP_CLAUSE_LASTPRIVATE */
235 4, /* OMP_CLAUSE_REDUCTION */
236 1, /* OMP_CLAUSE_COPYIN */
237 1, /* OMP_CLAUSE_COPYPRIVATE */
238 1, /* OMP_CLAUSE_IF */
239 1, /* OMP_CLAUSE_NUM_THREADS */
240 1, /* OMP_CLAUSE_SCHEDULE */
241 0, /* OMP_CLAUSE_NOWAIT */
242 0, /* OMP_CLAUSE_ORDERED */
243 0, /* OMP_CLAUSE_DEFAULT */
244 3, /* OMP_CLAUSE_COLLAPSE */
245 0, /* OMP_CLAUSE_UNTIED */
246 1, /* OMP_CLAUSE_FINAL */
247 0 /* OMP_CLAUSE_MERGEABLE */
250 const char * const omp_clause_code_name[] =
252 "error_clause",
253 "private",
254 "shared",
255 "firstprivate",
256 "lastprivate",
257 "reduction",
258 "copyin",
259 "copyprivate",
260 "if",
261 "num_threads",
262 "schedule",
263 "nowait",
264 "ordered",
265 "default",
266 "collapse",
267 "untied",
268 "final",
269 "mergeable"
273 /* Return the tree node structure used by tree code CODE. */
275 static inline enum tree_node_structure_enum
276 tree_node_structure_for_code (enum tree_code code)
278 switch (TREE_CODE_CLASS (code))
280 case tcc_declaration:
282 switch (code)
284 case FIELD_DECL:
285 return TS_FIELD_DECL;
286 case PARM_DECL:
287 return TS_PARM_DECL;
288 case VAR_DECL:
289 return TS_VAR_DECL;
290 case LABEL_DECL:
291 return TS_LABEL_DECL;
292 case RESULT_DECL:
293 return TS_RESULT_DECL;
294 case DEBUG_EXPR_DECL:
295 return TS_DECL_WRTL;
296 case CONST_DECL:
297 return TS_CONST_DECL;
298 case TYPE_DECL:
299 return TS_TYPE_DECL;
300 case FUNCTION_DECL:
301 return TS_FUNCTION_DECL;
302 case TRANSLATION_UNIT_DECL:
303 return TS_TRANSLATION_UNIT_DECL;
304 default:
305 return TS_DECL_NON_COMMON;
308 case tcc_type:
309 return TS_TYPE_NON_COMMON;
310 case tcc_reference:
311 case tcc_comparison:
312 case tcc_unary:
313 case tcc_binary:
314 case tcc_expression:
315 case tcc_statement:
316 case tcc_vl_exp:
317 return TS_EXP;
318 default: /* tcc_constant and tcc_exceptional */
319 break;
321 switch (code)
323 /* tcc_constant cases. */
324 case INTEGER_CST: return TS_INT_CST;
325 case REAL_CST: return TS_REAL_CST;
326 case FIXED_CST: return TS_FIXED_CST;
327 case COMPLEX_CST: return TS_COMPLEX;
328 case VECTOR_CST: return TS_VECTOR;
329 case STRING_CST: return TS_STRING;
330 /* tcc_exceptional cases. */
331 case ERROR_MARK: return TS_COMMON;
332 case IDENTIFIER_NODE: return TS_IDENTIFIER;
333 case TREE_LIST: return TS_LIST;
334 case TREE_VEC: return TS_VEC;
335 case SSA_NAME: return TS_SSA_NAME;
336 case PLACEHOLDER_EXPR: return TS_COMMON;
337 case STATEMENT_LIST: return TS_STATEMENT_LIST;
338 case BLOCK: return TS_BLOCK;
339 case CONSTRUCTOR: return TS_CONSTRUCTOR;
340 case TREE_BINFO: return TS_BINFO;
341 case OMP_CLAUSE: return TS_OMP_CLAUSE;
342 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
343 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
345 default:
346 gcc_unreachable ();
351 /* Initialize tree_contains_struct to describe the hierarchy of tree
352 nodes. */
354 static void
355 initialize_tree_contains_struct (void)
357 unsigned i;
359 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
361 enum tree_code code;
362 enum tree_node_structure_enum ts_code;
364 code = (enum tree_code) i;
365 ts_code = tree_node_structure_for_code (code);
367 /* Mark the TS structure itself. */
368 tree_contains_struct[code][ts_code] = 1;
370 /* Mark all the structures that TS is derived from. */
371 switch (ts_code)
373 case TS_TYPED:
374 case TS_BLOCK:
375 MARK_TS_BASE (code);
376 break;
378 case TS_COMMON:
379 case TS_INT_CST:
380 case TS_REAL_CST:
381 case TS_FIXED_CST:
382 case TS_VECTOR:
383 case TS_STRING:
384 case TS_COMPLEX:
385 case TS_SSA_NAME:
386 case TS_CONSTRUCTOR:
387 case TS_EXP:
388 case TS_STATEMENT_LIST:
389 MARK_TS_TYPED (code);
390 break;
392 case TS_IDENTIFIER:
393 case TS_DECL_MINIMAL:
394 case TS_TYPE_COMMON:
395 case TS_LIST:
396 case TS_VEC:
397 case TS_BINFO:
398 case TS_OMP_CLAUSE:
399 case TS_OPTIMIZATION:
400 case TS_TARGET_OPTION:
401 MARK_TS_COMMON (code);
402 break;
404 case TS_TYPE_WITH_LANG_SPECIFIC:
405 MARK_TS_TYPE_COMMON (code);
406 break;
408 case TS_TYPE_NON_COMMON:
409 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
410 break;
412 case TS_DECL_COMMON:
413 MARK_TS_DECL_MINIMAL (code);
414 break;
416 case TS_DECL_WRTL:
417 case TS_CONST_DECL:
418 MARK_TS_DECL_COMMON (code);
419 break;
421 case TS_DECL_NON_COMMON:
422 MARK_TS_DECL_WITH_VIS (code);
423 break;
425 case TS_DECL_WITH_VIS:
426 case TS_PARM_DECL:
427 case TS_LABEL_DECL:
428 case TS_RESULT_DECL:
429 MARK_TS_DECL_WRTL (code);
430 break;
432 case TS_FIELD_DECL:
433 MARK_TS_DECL_COMMON (code);
434 break;
436 case TS_VAR_DECL:
437 MARK_TS_DECL_WITH_VIS (code);
438 break;
440 case TS_TYPE_DECL:
441 case TS_FUNCTION_DECL:
442 MARK_TS_DECL_NON_COMMON (code);
443 break;
445 case TS_TRANSLATION_UNIT_DECL:
446 MARK_TS_DECL_COMMON (code);
447 break;
449 default:
450 gcc_unreachable ();
454 /* Basic consistency checks for attributes used in fold. */
455 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
456 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
457 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
458 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
459 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
460 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
461 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
462 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
463 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
464 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
465 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
466 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
467 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
468 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
469 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
470 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
471 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
472 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
473 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
474 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
475 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
476 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
477 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
478 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
479 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
480 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
481 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
482 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
483 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
484 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
485 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
486 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
487 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
488 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
489 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
490 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
491 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
492 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
496 /* Init tree.c. */
498 void
499 init_ttree (void)
501 /* Initialize the hash table of types. */
502 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
503 type_hash_eq, 0);
505 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
506 tree_decl_map_eq, 0);
508 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
509 tree_decl_map_eq, 0);
510 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
511 tree_priority_map_eq, 0);
513 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
514 int_cst_hash_eq, NULL);
516 int_cst_node = make_node (INTEGER_CST);
518 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
519 cl_option_hash_eq, NULL);
521 cl_optimization_node = make_node (OPTIMIZATION_NODE);
522 cl_target_option_node = make_node (TARGET_OPTION_NODE);
524 /* Initialize the tree_contains_struct array. */
525 initialize_tree_contains_struct ();
526 lang_hooks.init_ts ();
530 /* The name of the object as the assembler will see it (but before any
531 translations made by ASM_OUTPUT_LABELREF). Often this is the same
532 as DECL_NAME. It is an IDENTIFIER_NODE. */
533 tree
534 decl_assembler_name (tree decl)
536 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
537 lang_hooks.set_decl_assembler_name (decl);
538 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
541 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
543 bool
544 decl_assembler_name_equal (tree decl, const_tree asmname)
546 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
547 const char *decl_str;
548 const char *asmname_str;
549 bool test = false;
551 if (decl_asmname == asmname)
552 return true;
554 decl_str = IDENTIFIER_POINTER (decl_asmname);
555 asmname_str = IDENTIFIER_POINTER (asmname);
558 /* If the target assembler name was set by the user, things are trickier.
559 We have a leading '*' to begin with. After that, it's arguable what
560 is the correct thing to do with -fleading-underscore. Arguably, we've
561 historically been doing the wrong thing in assemble_alias by always
562 printing the leading underscore. Since we're not changing that, make
563 sure user_label_prefix follows the '*' before matching. */
564 if (decl_str[0] == '*')
566 size_t ulp_len = strlen (user_label_prefix);
568 decl_str ++;
570 if (ulp_len == 0)
571 test = true;
572 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
573 decl_str += ulp_len, test=true;
574 else
575 decl_str --;
577 if (asmname_str[0] == '*')
579 size_t ulp_len = strlen (user_label_prefix);
581 asmname_str ++;
583 if (ulp_len == 0)
584 test = true;
585 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
586 asmname_str += ulp_len, test=true;
587 else
588 asmname_str --;
591 if (!test)
592 return false;
593 return strcmp (decl_str, asmname_str) == 0;
596 /* Hash asmnames ignoring the user specified marks. */
598 hashval_t
599 decl_assembler_name_hash (const_tree asmname)
601 if (IDENTIFIER_POINTER (asmname)[0] == '*')
603 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
604 size_t ulp_len = strlen (user_label_prefix);
606 if (ulp_len == 0)
608 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
609 decl_str += ulp_len;
611 return htab_hash_string (decl_str);
614 return htab_hash_string (IDENTIFIER_POINTER (asmname));
617 /* Compute the number of bytes occupied by a tree with code CODE.
618 This function cannot be used for nodes that have variable sizes,
619 including TREE_VEC, STRING_CST, and CALL_EXPR. */
620 size_t
621 tree_code_size (enum tree_code code)
623 switch (TREE_CODE_CLASS (code))
625 case tcc_declaration: /* A decl node */
627 switch (code)
629 case FIELD_DECL:
630 return sizeof (struct tree_field_decl);
631 case PARM_DECL:
632 return sizeof (struct tree_parm_decl);
633 case VAR_DECL:
634 return sizeof (struct tree_var_decl);
635 case LABEL_DECL:
636 return sizeof (struct tree_label_decl);
637 case RESULT_DECL:
638 return sizeof (struct tree_result_decl);
639 case CONST_DECL:
640 return sizeof (struct tree_const_decl);
641 case TYPE_DECL:
642 return sizeof (struct tree_type_decl);
643 case FUNCTION_DECL:
644 return sizeof (struct tree_function_decl);
645 case DEBUG_EXPR_DECL:
646 return sizeof (struct tree_decl_with_rtl);
647 default:
648 return sizeof (struct tree_decl_non_common);
652 case tcc_type: /* a type node */
653 return sizeof (struct tree_type_non_common);
655 case tcc_reference: /* a reference */
656 case tcc_expression: /* an expression */
657 case tcc_statement: /* an expression with side effects */
658 case tcc_comparison: /* a comparison expression */
659 case tcc_unary: /* a unary arithmetic expression */
660 case tcc_binary: /* a binary arithmetic expression */
661 return (sizeof (struct tree_exp)
662 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
664 case tcc_constant: /* a constant */
665 switch (code)
667 case INTEGER_CST: return sizeof (struct tree_int_cst);
668 case REAL_CST: return sizeof (struct tree_real_cst);
669 case FIXED_CST: return sizeof (struct tree_fixed_cst);
670 case COMPLEX_CST: return sizeof (struct tree_complex);
671 case VECTOR_CST: return sizeof (struct tree_vector);
672 case STRING_CST: gcc_unreachable ();
673 default:
674 return lang_hooks.tree_size (code);
677 case tcc_exceptional: /* something random, like an identifier. */
678 switch (code)
680 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
681 case TREE_LIST: return sizeof (struct tree_list);
683 case ERROR_MARK:
684 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
686 case TREE_VEC:
687 case OMP_CLAUSE: gcc_unreachable ();
689 case SSA_NAME: return sizeof (struct tree_ssa_name);
691 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
692 case BLOCK: return sizeof (struct tree_block);
693 case CONSTRUCTOR: return sizeof (struct tree_constructor);
694 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
695 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
697 default:
698 return lang_hooks.tree_size (code);
701 default:
702 gcc_unreachable ();
706 /* Compute the number of bytes occupied by NODE. This routine only
707 looks at TREE_CODE, except for those nodes that have variable sizes. */
708 size_t
709 tree_size (const_tree node)
711 const enum tree_code code = TREE_CODE (node);
712 switch (code)
714 case TREE_BINFO:
715 return (offsetof (struct tree_binfo, base_binfos)
716 + vec<tree, va_gc>
717 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
719 case TREE_VEC:
720 return (sizeof (struct tree_vec)
721 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
723 case VECTOR_CST:
724 return (sizeof (struct tree_vector)
725 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
727 case STRING_CST:
728 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
730 case OMP_CLAUSE:
731 return (sizeof (struct tree_omp_clause)
732 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
733 * sizeof (tree));
735 default:
736 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
737 return (sizeof (struct tree_exp)
738 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
739 else
740 return tree_code_size (code);
744 /* Record interesting allocation statistics for a tree node with CODE
745 and LENGTH. */
747 static void
748 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
749 size_t length ATTRIBUTE_UNUSED)
751 enum tree_code_class type = TREE_CODE_CLASS (code);
752 tree_node_kind kind;
754 if (!GATHER_STATISTICS)
755 return;
757 switch (type)
759 case tcc_declaration: /* A decl node */
760 kind = d_kind;
761 break;
763 case tcc_type: /* a type node */
764 kind = t_kind;
765 break;
767 case tcc_statement: /* an expression with side effects */
768 kind = s_kind;
769 break;
771 case tcc_reference: /* a reference */
772 kind = r_kind;
773 break;
775 case tcc_expression: /* an expression */
776 case tcc_comparison: /* a comparison expression */
777 case tcc_unary: /* a unary arithmetic expression */
778 case tcc_binary: /* a binary arithmetic expression */
779 kind = e_kind;
780 break;
782 case tcc_constant: /* a constant */
783 kind = c_kind;
784 break;
786 case tcc_exceptional: /* something random, like an identifier. */
787 switch (code)
789 case IDENTIFIER_NODE:
790 kind = id_kind;
791 break;
793 case TREE_VEC:
794 kind = vec_kind;
795 break;
797 case TREE_BINFO:
798 kind = binfo_kind;
799 break;
801 case SSA_NAME:
802 kind = ssa_name_kind;
803 break;
805 case BLOCK:
806 kind = b_kind;
807 break;
809 case CONSTRUCTOR:
810 kind = constr_kind;
811 break;
813 case OMP_CLAUSE:
814 kind = omp_clause_kind;
815 break;
817 default:
818 kind = x_kind;
819 break;
821 break;
823 case tcc_vl_exp:
824 kind = e_kind;
825 break;
827 default:
828 gcc_unreachable ();
831 tree_code_counts[(int) code]++;
832 tree_node_counts[(int) kind]++;
833 tree_node_sizes[(int) kind] += length;
836 /* Allocate and return a new UID from the DECL_UID namespace. */
839 allocate_decl_uid (void)
841 return next_decl_uid++;
844 /* Return a newly allocated node of code CODE. For decl and type
845 nodes, some other fields are initialized. The rest of the node is
846 initialized to zero. This function cannot be used for TREE_VEC or
847 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
849 Achoo! I got a code in the node. */
851 tree
852 make_node_stat (enum tree_code code MEM_STAT_DECL)
854 tree t;
855 enum tree_code_class type = TREE_CODE_CLASS (code);
856 size_t length = tree_code_size (code);
858 record_node_allocation_statistics (code, length);
860 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
861 TREE_SET_CODE (t, code);
863 switch (type)
865 case tcc_statement:
866 TREE_SIDE_EFFECTS (t) = 1;
867 break;
869 case tcc_declaration:
870 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
872 if (code == FUNCTION_DECL)
874 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
875 DECL_MODE (t) = FUNCTION_MODE;
877 else
878 DECL_ALIGN (t) = 1;
880 DECL_SOURCE_LOCATION (t) = input_location;
881 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
882 DECL_UID (t) = --next_debug_decl_uid;
883 else
885 DECL_UID (t) = allocate_decl_uid ();
886 SET_DECL_PT_UID (t, -1);
888 if (TREE_CODE (t) == LABEL_DECL)
889 LABEL_DECL_UID (t) = -1;
891 break;
893 case tcc_type:
894 TYPE_UID (t) = next_type_uid++;
895 TYPE_ALIGN (t) = BITS_PER_UNIT;
896 TYPE_USER_ALIGN (t) = 0;
897 TYPE_MAIN_VARIANT (t) = t;
898 TYPE_CANONICAL (t) = t;
900 /* Default to no attributes for type, but let target change that. */
901 TYPE_ATTRIBUTES (t) = NULL_TREE;
902 targetm.set_default_type_attributes (t);
904 /* We have not yet computed the alias set for this type. */
905 TYPE_ALIAS_SET (t) = -1;
906 break;
908 case tcc_constant:
909 TREE_CONSTANT (t) = 1;
910 break;
912 case tcc_expression:
913 switch (code)
915 case INIT_EXPR:
916 case MODIFY_EXPR:
917 case VA_ARG_EXPR:
918 case PREDECREMENT_EXPR:
919 case PREINCREMENT_EXPR:
920 case POSTDECREMENT_EXPR:
921 case POSTINCREMENT_EXPR:
922 /* All of these have side-effects, no matter what their
923 operands are. */
924 TREE_SIDE_EFFECTS (t) = 1;
925 break;
927 default:
928 break;
930 break;
932 default:
933 /* Other classes need no special treatment. */
934 break;
937 return t;
940 /* Return a new node with the same contents as NODE except that its
941 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
943 tree
944 copy_node_stat (tree node MEM_STAT_DECL)
946 tree t;
947 enum tree_code code = TREE_CODE (node);
948 size_t length;
950 gcc_assert (code != STATEMENT_LIST);
952 length = tree_size (node);
953 record_node_allocation_statistics (code, length);
954 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
955 memcpy (t, node, length);
957 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
958 TREE_CHAIN (t) = 0;
959 TREE_ASM_WRITTEN (t) = 0;
960 TREE_VISITED (t) = 0;
962 if (TREE_CODE_CLASS (code) == tcc_declaration)
964 if (code == DEBUG_EXPR_DECL)
965 DECL_UID (t) = --next_debug_decl_uid;
966 else
968 DECL_UID (t) = allocate_decl_uid ();
969 if (DECL_PT_UID_SET_P (node))
970 SET_DECL_PT_UID (t, DECL_PT_UID (node));
972 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
973 && DECL_HAS_VALUE_EXPR_P (node))
975 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
976 DECL_HAS_VALUE_EXPR_P (t) = 1;
978 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
980 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
981 DECL_HAS_INIT_PRIORITY_P (t) = 1;
983 if (TREE_CODE (node) == FUNCTION_DECL)
984 DECL_STRUCT_FUNCTION (t) = NULL;
986 else if (TREE_CODE_CLASS (code) == tcc_type)
988 TYPE_UID (t) = next_type_uid++;
989 /* The following is so that the debug code for
990 the copy is different from the original type.
991 The two statements usually duplicate each other
992 (because they clear fields of the same union),
993 but the optimizer should catch that. */
994 TYPE_SYMTAB_POINTER (t) = 0;
995 TYPE_SYMTAB_ADDRESS (t) = 0;
997 /* Do not copy the values cache. */
998 if (TYPE_CACHED_VALUES_P(t))
1000 TYPE_CACHED_VALUES_P (t) = 0;
1001 TYPE_CACHED_VALUES (t) = NULL_TREE;
1005 return t;
1008 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1009 For example, this can copy a list made of TREE_LIST nodes. */
1011 tree
1012 copy_list (tree list)
1014 tree head;
1015 tree prev, next;
1017 if (list == 0)
1018 return 0;
1020 head = prev = copy_node (list);
1021 next = TREE_CHAIN (list);
1022 while (next)
1024 TREE_CHAIN (prev) = copy_node (next);
1025 prev = TREE_CHAIN (prev);
1026 next = TREE_CHAIN (next);
1028 return head;
1032 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1034 tree
1035 build_int_cst (tree type, HOST_WIDE_INT low)
1037 /* Support legacy code. */
1038 if (!type)
1039 type = integer_type_node;
1041 return double_int_to_tree (type, double_int::from_shwi (low));
1044 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1046 tree
1047 build_int_cst_type (tree type, HOST_WIDE_INT low)
1049 gcc_assert (type);
1051 return double_int_to_tree (type, double_int::from_shwi (low));
1054 /* Constructs tree in type TYPE from with value given by CST. Signedness
1055 of CST is assumed to be the same as the signedness of TYPE. */
1057 tree
1058 double_int_to_tree (tree type, double_int cst)
1060 bool sign_extended_type = !TYPE_UNSIGNED (type);
1062 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1064 return build_int_cst_wide (type, cst.low, cst.high);
1067 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1068 to be the same as the signedness of TYPE. */
1070 bool
1071 double_int_fits_to_tree_p (const_tree type, double_int cst)
1073 bool sign_extended_type = !TYPE_UNSIGNED (type);
1075 double_int ext
1076 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1078 return cst == ext;
1081 /* We force the double_int CST to the range of the type TYPE by sign or
1082 zero extending it. OVERFLOWABLE indicates if we are interested in
1083 overflow of the value, when >0 we are only interested in signed
1084 overflow, for <0 we are interested in any overflow. OVERFLOWED
1085 indicates whether overflow has already occurred. CONST_OVERFLOWED
1086 indicates whether constant overflow has already occurred. We force
1087 T's value to be within range of T's type (by setting to 0 or 1 all
1088 the bits outside the type's range). We set TREE_OVERFLOWED if,
1089 OVERFLOWED is nonzero,
1090 or OVERFLOWABLE is >0 and signed overflow occurs
1091 or OVERFLOWABLE is <0 and any overflow occurs
1092 We return a new tree node for the extended double_int. The node
1093 is shared if no overflow flags are set. */
1096 tree
1097 force_fit_type_double (tree type, double_int cst, int overflowable,
1098 bool overflowed)
1100 bool sign_extended_type = !TYPE_UNSIGNED (type);
1102 /* If we need to set overflow flags, return a new unshared node. */
1103 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1105 if (overflowed
1106 || overflowable < 0
1107 || (overflowable > 0 && sign_extended_type))
1109 tree t = make_node (INTEGER_CST);
1110 TREE_INT_CST (t)
1111 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1112 TREE_TYPE (t) = type;
1113 TREE_OVERFLOW (t) = 1;
1114 return t;
1118 /* Else build a shared node. */
1119 return double_int_to_tree (type, cst);
1122 /* These are the hash table functions for the hash table of INTEGER_CST
1123 nodes of a sizetype. */
1125 /* Return the hash code code X, an INTEGER_CST. */
1127 static hashval_t
1128 int_cst_hash_hash (const void *x)
1130 const_tree const t = (const_tree) x;
1132 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1133 ^ htab_hash_pointer (TREE_TYPE (t)));
1136 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1137 is the same as that given by *Y, which is the same. */
1139 static int
1140 int_cst_hash_eq (const void *x, const void *y)
1142 const_tree const xt = (const_tree) x;
1143 const_tree const yt = (const_tree) y;
1145 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1146 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1147 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1150 /* Create an INT_CST node of TYPE and value HI:LOW.
1151 The returned node is always shared. For small integers we use a
1152 per-type vector cache, for larger ones we use a single hash table. */
1154 tree
1155 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1157 tree t;
1158 int ix = -1;
1159 int limit = 0;
1161 gcc_assert (type);
1163 switch (TREE_CODE (type))
1165 case NULLPTR_TYPE:
1166 gcc_assert (hi == 0 && low == 0);
1167 /* Fallthru. */
1169 case POINTER_TYPE:
1170 case REFERENCE_TYPE:
1171 /* Cache NULL pointer. */
1172 if (!hi && !low)
1174 limit = 1;
1175 ix = 0;
1177 break;
1179 case BOOLEAN_TYPE:
1180 /* Cache false or true. */
1181 limit = 2;
1182 if (!hi && low < 2)
1183 ix = low;
1184 break;
1186 case INTEGER_TYPE:
1187 case OFFSET_TYPE:
1188 if (TYPE_UNSIGNED (type))
1190 /* Cache 0..N */
1191 limit = INTEGER_SHARE_LIMIT;
1192 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1193 ix = low;
1195 else
1197 /* Cache -1..N */
1198 limit = INTEGER_SHARE_LIMIT + 1;
1199 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1200 ix = low + 1;
1201 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1202 ix = 0;
1204 break;
1206 case ENUMERAL_TYPE:
1207 break;
1209 default:
1210 gcc_unreachable ();
1213 if (ix >= 0)
1215 /* Look for it in the type's vector of small shared ints. */
1216 if (!TYPE_CACHED_VALUES_P (type))
1218 TYPE_CACHED_VALUES_P (type) = 1;
1219 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1222 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1223 if (t)
1225 /* Make sure no one is clobbering the shared constant. */
1226 gcc_assert (TREE_TYPE (t) == type);
1227 gcc_assert (TREE_INT_CST_LOW (t) == low);
1228 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1230 else
1232 /* Create a new shared int. */
1233 t = make_node (INTEGER_CST);
1235 TREE_INT_CST_LOW (t) = low;
1236 TREE_INT_CST_HIGH (t) = hi;
1237 TREE_TYPE (t) = type;
1239 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1242 else
1244 /* Use the cache of larger shared ints. */
1245 void **slot;
1247 TREE_INT_CST_LOW (int_cst_node) = low;
1248 TREE_INT_CST_HIGH (int_cst_node) = hi;
1249 TREE_TYPE (int_cst_node) = type;
1251 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1252 t = (tree) *slot;
1253 if (!t)
1255 /* Insert this one into the hash table. */
1256 t = int_cst_node;
1257 *slot = t;
1258 /* Make a new node for next time round. */
1259 int_cst_node = make_node (INTEGER_CST);
1263 return t;
1266 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1267 and the rest are zeros. */
1269 tree
1270 build_low_bits_mask (tree type, unsigned bits)
1272 double_int mask;
1274 gcc_assert (bits <= TYPE_PRECISION (type));
1276 if (bits == TYPE_PRECISION (type)
1277 && !TYPE_UNSIGNED (type))
1278 /* Sign extended all-ones mask. */
1279 mask = double_int_minus_one;
1280 else
1281 mask = double_int::mask (bits);
1283 return build_int_cst_wide (type, mask.low, mask.high);
1286 /* Checks that X is integer constant that can be expressed in (unsigned)
1287 HOST_WIDE_INT without loss of precision. */
1289 bool
1290 cst_and_fits_in_hwi (const_tree x)
1292 if (TREE_CODE (x) != INTEGER_CST)
1293 return false;
1295 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1296 return false;
1298 return (TREE_INT_CST_HIGH (x) == 0
1299 || TREE_INT_CST_HIGH (x) == -1);
1302 /* Build a newly constructed TREE_VEC node of length LEN. */
1304 tree
1305 make_vector_stat (unsigned len MEM_STAT_DECL)
1307 tree t;
1308 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1310 record_node_allocation_statistics (VECTOR_CST, length);
1312 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1314 TREE_SET_CODE (t, VECTOR_CST);
1315 TREE_CONSTANT (t) = 1;
1317 return t;
1320 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1321 are in a list pointed to by VALS. */
1323 tree
1324 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1326 int over = 0;
1327 unsigned cnt = 0;
1328 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1329 TREE_TYPE (v) = type;
1331 /* Iterate through elements and check for overflow. */
1332 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1334 tree value = vals[cnt];
1336 VECTOR_CST_ELT (v, cnt) = value;
1338 /* Don't crash if we get an address constant. */
1339 if (!CONSTANT_CLASS_P (value))
1340 continue;
1342 over |= TREE_OVERFLOW (value);
1345 TREE_OVERFLOW (v) = over;
1346 return v;
1349 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1350 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1352 tree
1353 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1355 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1356 unsigned HOST_WIDE_INT idx;
1357 tree value;
1359 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1360 vec[idx] = value;
1361 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1362 vec[idx] = build_zero_cst (TREE_TYPE (type));
1364 return build_vector (type, vec);
1367 /* Build a vector of type VECTYPE where all the elements are SCs. */
1368 tree
1369 build_vector_from_val (tree vectype, tree sc)
1371 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1373 if (sc == error_mark_node)
1374 return sc;
1376 /* Verify that the vector type is suitable for SC. Note that there
1377 is some inconsistency in the type-system with respect to restrict
1378 qualifications of pointers. Vector types always have a main-variant
1379 element type and the qualification is applied to the vector-type.
1380 So TREE_TYPE (vector-type) does not return a properly qualified
1381 vector element-type. */
1382 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1383 TREE_TYPE (vectype)));
1385 if (CONSTANT_CLASS_P (sc))
1387 tree *v = XALLOCAVEC (tree, nunits);
1388 for (i = 0; i < nunits; ++i)
1389 v[i] = sc;
1390 return build_vector (vectype, v);
1392 else
1394 vec<constructor_elt, va_gc> *v;
1395 vec_alloc (v, nunits);
1396 for (i = 0; i < nunits; ++i)
1397 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1398 return build_constructor (vectype, v);
1402 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1403 are in the vec pointed to by VALS. */
1404 tree
1405 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1407 tree c = make_node (CONSTRUCTOR);
1408 unsigned int i;
1409 constructor_elt *elt;
1410 bool constant_p = true;
1411 bool side_effects_p = false;
1413 TREE_TYPE (c) = type;
1414 CONSTRUCTOR_ELTS (c) = vals;
1416 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1418 /* Mostly ctors will have elts that don't have side-effects, so
1419 the usual case is to scan all the elements. Hence a single
1420 loop for both const and side effects, rather than one loop
1421 each (with early outs). */
1422 if (!TREE_CONSTANT (elt->value))
1423 constant_p = false;
1424 if (TREE_SIDE_EFFECTS (elt->value))
1425 side_effects_p = true;
1428 TREE_SIDE_EFFECTS (c) = side_effects_p;
1429 TREE_CONSTANT (c) = constant_p;
1431 return c;
1434 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1435 INDEX and VALUE. */
1436 tree
1437 build_constructor_single (tree type, tree index, tree value)
1439 vec<constructor_elt, va_gc> *v;
1440 constructor_elt elt = {index, value};
1442 vec_alloc (v, 1);
1443 v->quick_push (elt);
1445 return build_constructor (type, v);
1449 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1450 are in a list pointed to by VALS. */
1451 tree
1452 build_constructor_from_list (tree type, tree vals)
1454 tree t;
1455 vec<constructor_elt, va_gc> *v = NULL;
1457 if (vals)
1459 vec_alloc (v, list_length (vals));
1460 for (t = vals; t; t = TREE_CHAIN (t))
1461 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1464 return build_constructor (type, v);
1467 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1469 tree
1470 build_fixed (tree type, FIXED_VALUE_TYPE f)
1472 tree v;
1473 FIXED_VALUE_TYPE *fp;
1475 v = make_node (FIXED_CST);
1476 fp = ggc_alloc_fixed_value ();
1477 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1479 TREE_TYPE (v) = type;
1480 TREE_FIXED_CST_PTR (v) = fp;
1481 return v;
1484 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1486 tree
1487 build_real (tree type, REAL_VALUE_TYPE d)
1489 tree v;
1490 REAL_VALUE_TYPE *dp;
1491 int overflow = 0;
1493 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1494 Consider doing it via real_convert now. */
1496 v = make_node (REAL_CST);
1497 dp = ggc_alloc_real_value ();
1498 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1500 TREE_TYPE (v) = type;
1501 TREE_REAL_CST_PTR (v) = dp;
1502 TREE_OVERFLOW (v) = overflow;
1503 return v;
1506 /* Return a new REAL_CST node whose type is TYPE
1507 and whose value is the integer value of the INTEGER_CST node I. */
1509 REAL_VALUE_TYPE
1510 real_value_from_int_cst (const_tree type, const_tree i)
1512 REAL_VALUE_TYPE d;
1514 /* Clear all bits of the real value type so that we can later do
1515 bitwise comparisons to see if two values are the same. */
1516 memset (&d, 0, sizeof d);
1518 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1519 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1520 TYPE_UNSIGNED (TREE_TYPE (i)));
1521 return d;
1524 /* Given a tree representing an integer constant I, return a tree
1525 representing the same value as a floating-point constant of type TYPE. */
1527 tree
1528 build_real_from_int_cst (tree type, const_tree i)
1530 tree v;
1531 int overflow = TREE_OVERFLOW (i);
1533 v = build_real (type, real_value_from_int_cst (type, i));
1535 TREE_OVERFLOW (v) |= overflow;
1536 return v;
1539 /* Return a newly constructed STRING_CST node whose value is
1540 the LEN characters at STR.
1541 Note that for a C string literal, LEN should include the trailing NUL.
1542 The TREE_TYPE is not initialized. */
1544 tree
1545 build_string (int len, const char *str)
1547 tree s;
1548 size_t length;
1550 /* Do not waste bytes provided by padding of struct tree_string. */
1551 length = len + offsetof (struct tree_string, str) + 1;
1553 record_node_allocation_statistics (STRING_CST, length);
1555 s = ggc_alloc_tree_node (length);
1557 memset (s, 0, sizeof (struct tree_typed));
1558 TREE_SET_CODE (s, STRING_CST);
1559 TREE_CONSTANT (s) = 1;
1560 TREE_STRING_LENGTH (s) = len;
1561 memcpy (s->string.str, str, len);
1562 s->string.str[len] = '\0';
1564 return s;
1567 /* Return a newly constructed COMPLEX_CST node whose value is
1568 specified by the real and imaginary parts REAL and IMAG.
1569 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1570 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1572 tree
1573 build_complex (tree type, tree real, tree imag)
1575 tree t = make_node (COMPLEX_CST);
1577 TREE_REALPART (t) = real;
1578 TREE_IMAGPART (t) = imag;
1579 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1580 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1581 return t;
1584 /* Return a constant of arithmetic type TYPE which is the
1585 multiplicative identity of the set TYPE. */
1587 tree
1588 build_one_cst (tree type)
1590 switch (TREE_CODE (type))
1592 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1593 case POINTER_TYPE: case REFERENCE_TYPE:
1594 case OFFSET_TYPE:
1595 return build_int_cst (type, 1);
1597 case REAL_TYPE:
1598 return build_real (type, dconst1);
1600 case FIXED_POINT_TYPE:
1601 /* We can only generate 1 for accum types. */
1602 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1603 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1605 case VECTOR_TYPE:
1607 tree scalar = build_one_cst (TREE_TYPE (type));
1609 return build_vector_from_val (type, scalar);
1612 case COMPLEX_TYPE:
1613 return build_complex (type,
1614 build_one_cst (TREE_TYPE (type)),
1615 build_zero_cst (TREE_TYPE (type)));
1617 default:
1618 gcc_unreachable ();
1622 /* Build 0 constant of type TYPE. This is used by constructor folding
1623 and thus the constant should be represented in memory by
1624 zero(es). */
1626 tree
1627 build_zero_cst (tree type)
1629 switch (TREE_CODE (type))
1631 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1632 case POINTER_TYPE: case REFERENCE_TYPE:
1633 case OFFSET_TYPE: case NULLPTR_TYPE:
1634 return build_int_cst (type, 0);
1636 case REAL_TYPE:
1637 return build_real (type, dconst0);
1639 case FIXED_POINT_TYPE:
1640 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1642 case VECTOR_TYPE:
1644 tree scalar = build_zero_cst (TREE_TYPE (type));
1646 return build_vector_from_val (type, scalar);
1649 case COMPLEX_TYPE:
1651 tree zero = build_zero_cst (TREE_TYPE (type));
1653 return build_complex (type, zero, zero);
1656 default:
1657 if (!AGGREGATE_TYPE_P (type))
1658 return fold_convert (type, integer_zero_node);
1659 return build_constructor (type, NULL);
1664 /* Build a BINFO with LEN language slots. */
1666 tree
1667 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1669 tree t;
1670 size_t length = (offsetof (struct tree_binfo, base_binfos)
1671 + vec<tree, va_gc>::embedded_size (base_binfos));
1673 record_node_allocation_statistics (TREE_BINFO, length);
1675 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1677 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1679 TREE_SET_CODE (t, TREE_BINFO);
1681 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1683 return t;
1686 /* Create a CASE_LABEL_EXPR tree node and return it. */
1688 tree
1689 build_case_label (tree low_value, tree high_value, tree label_decl)
1691 tree t = make_node (CASE_LABEL_EXPR);
1693 TREE_TYPE (t) = void_type_node;
1694 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1696 CASE_LOW (t) = low_value;
1697 CASE_HIGH (t) = high_value;
1698 CASE_LABEL (t) = label_decl;
1699 CASE_CHAIN (t) = NULL_TREE;
1701 return t;
1704 /* Build a newly constructed TREE_VEC node of length LEN. */
1706 tree
1707 make_tree_vec_stat (int len MEM_STAT_DECL)
1709 tree t;
1710 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1712 record_node_allocation_statistics (TREE_VEC, length);
1714 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1716 TREE_SET_CODE (t, TREE_VEC);
1717 TREE_VEC_LENGTH (t) = len;
1719 return t;
1722 /* Return 1 if EXPR is the integer constant zero or a complex constant
1723 of zero. */
1726 integer_zerop (const_tree expr)
1728 STRIP_NOPS (expr);
1730 switch (TREE_CODE (expr))
1732 case INTEGER_CST:
1733 return (TREE_INT_CST_LOW (expr) == 0
1734 && TREE_INT_CST_HIGH (expr) == 0);
1735 case COMPLEX_CST:
1736 return (integer_zerop (TREE_REALPART (expr))
1737 && integer_zerop (TREE_IMAGPART (expr)));
1738 case VECTOR_CST:
1740 unsigned i;
1741 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1742 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1743 return false;
1744 return true;
1746 default:
1747 return false;
1751 /* Return 1 if EXPR is the integer constant one or the corresponding
1752 complex constant. */
1755 integer_onep (const_tree expr)
1757 STRIP_NOPS (expr);
1759 switch (TREE_CODE (expr))
1761 case INTEGER_CST:
1762 return (TREE_INT_CST_LOW (expr) == 1
1763 && TREE_INT_CST_HIGH (expr) == 0);
1764 case COMPLEX_CST:
1765 return (integer_onep (TREE_REALPART (expr))
1766 && integer_zerop (TREE_IMAGPART (expr)));
1767 case VECTOR_CST:
1769 unsigned i;
1770 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1771 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1772 return false;
1773 return true;
1775 default:
1776 return false;
1780 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1781 it contains. Likewise for the corresponding complex constant. */
1784 integer_all_onesp (const_tree expr)
1786 int prec;
1787 int uns;
1789 STRIP_NOPS (expr);
1791 if (TREE_CODE (expr) == COMPLEX_CST
1792 && integer_all_onesp (TREE_REALPART (expr))
1793 && integer_zerop (TREE_IMAGPART (expr)))
1794 return 1;
1796 else if (TREE_CODE (expr) == VECTOR_CST)
1798 unsigned i;
1799 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1800 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1801 return 0;
1802 return 1;
1805 else if (TREE_CODE (expr) != INTEGER_CST)
1806 return 0;
1808 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1809 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1810 && TREE_INT_CST_HIGH (expr) == -1)
1811 return 1;
1812 if (!uns)
1813 return 0;
1815 prec = TYPE_PRECISION (TREE_TYPE (expr));
1816 if (prec >= HOST_BITS_PER_WIDE_INT)
1818 HOST_WIDE_INT high_value;
1819 int shift_amount;
1821 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1823 /* Can not handle precisions greater than twice the host int size. */
1824 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1825 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1826 /* Shifting by the host word size is undefined according to the ANSI
1827 standard, so we must handle this as a special case. */
1828 high_value = -1;
1829 else
1830 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1832 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1833 && TREE_INT_CST_HIGH (expr) == high_value);
1835 else
1836 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1839 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1840 one bit on). */
1843 integer_pow2p (const_tree expr)
1845 int prec;
1846 unsigned HOST_WIDE_INT high, low;
1848 STRIP_NOPS (expr);
1850 if (TREE_CODE (expr) == COMPLEX_CST
1851 && integer_pow2p (TREE_REALPART (expr))
1852 && integer_zerop (TREE_IMAGPART (expr)))
1853 return 1;
1855 if (TREE_CODE (expr) != INTEGER_CST)
1856 return 0;
1858 prec = TYPE_PRECISION (TREE_TYPE (expr));
1859 high = TREE_INT_CST_HIGH (expr);
1860 low = TREE_INT_CST_LOW (expr);
1862 /* First clear all bits that are beyond the type's precision in case
1863 we've been sign extended. */
1865 if (prec == HOST_BITS_PER_DOUBLE_INT)
1867 else if (prec > HOST_BITS_PER_WIDE_INT)
1868 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1869 else
1871 high = 0;
1872 if (prec < HOST_BITS_PER_WIDE_INT)
1873 low &= ~((HOST_WIDE_INT) (-1) << prec);
1876 if (high == 0 && low == 0)
1877 return 0;
1879 return ((high == 0 && (low & (low - 1)) == 0)
1880 || (low == 0 && (high & (high - 1)) == 0));
1883 /* Return 1 if EXPR is an integer constant other than zero or a
1884 complex constant other than zero. */
1887 integer_nonzerop (const_tree expr)
1889 STRIP_NOPS (expr);
1891 return ((TREE_CODE (expr) == INTEGER_CST
1892 && (TREE_INT_CST_LOW (expr) != 0
1893 || TREE_INT_CST_HIGH (expr) != 0))
1894 || (TREE_CODE (expr) == COMPLEX_CST
1895 && (integer_nonzerop (TREE_REALPART (expr))
1896 || integer_nonzerop (TREE_IMAGPART (expr)))));
1899 /* Return 1 if EXPR is the fixed-point constant zero. */
1902 fixed_zerop (const_tree expr)
1904 return (TREE_CODE (expr) == FIXED_CST
1905 && TREE_FIXED_CST (expr).data.is_zero ());
1908 /* Return the power of two represented by a tree node known to be a
1909 power of two. */
1912 tree_log2 (const_tree expr)
1914 int prec;
1915 HOST_WIDE_INT high, low;
1917 STRIP_NOPS (expr);
1919 if (TREE_CODE (expr) == COMPLEX_CST)
1920 return tree_log2 (TREE_REALPART (expr));
1922 prec = TYPE_PRECISION (TREE_TYPE (expr));
1923 high = TREE_INT_CST_HIGH (expr);
1924 low = TREE_INT_CST_LOW (expr);
1926 /* First clear all bits that are beyond the type's precision in case
1927 we've been sign extended. */
1929 if (prec == HOST_BITS_PER_DOUBLE_INT)
1931 else if (prec > HOST_BITS_PER_WIDE_INT)
1932 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1933 else
1935 high = 0;
1936 if (prec < HOST_BITS_PER_WIDE_INT)
1937 low &= ~((HOST_WIDE_INT) (-1) << prec);
1940 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1941 : exact_log2 (low));
1944 /* Similar, but return the largest integer Y such that 2 ** Y is less
1945 than or equal to EXPR. */
1948 tree_floor_log2 (const_tree expr)
1950 int prec;
1951 HOST_WIDE_INT high, low;
1953 STRIP_NOPS (expr);
1955 if (TREE_CODE (expr) == COMPLEX_CST)
1956 return tree_log2 (TREE_REALPART (expr));
1958 prec = TYPE_PRECISION (TREE_TYPE (expr));
1959 high = TREE_INT_CST_HIGH (expr);
1960 low = TREE_INT_CST_LOW (expr);
1962 /* First clear all bits that are beyond the type's precision in case
1963 we've been sign extended. Ignore if type's precision hasn't been set
1964 since what we are doing is setting it. */
1966 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
1968 else if (prec > HOST_BITS_PER_WIDE_INT)
1969 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1970 else
1972 high = 0;
1973 if (prec < HOST_BITS_PER_WIDE_INT)
1974 low &= ~((HOST_WIDE_INT) (-1) << prec);
1977 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1978 : floor_log2 (low));
1981 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
1982 decimal float constants, so don't return 1 for them. */
1985 real_zerop (const_tree expr)
1987 STRIP_NOPS (expr);
1989 switch (TREE_CODE (expr))
1991 case REAL_CST:
1992 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
1993 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
1994 case COMPLEX_CST:
1995 return real_zerop (TREE_REALPART (expr))
1996 && real_zerop (TREE_IMAGPART (expr));
1997 case VECTOR_CST:
1999 unsigned i;
2000 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2001 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2002 return false;
2003 return true;
2005 default:
2006 return false;
2010 /* Return 1 if EXPR is the real constant one in real or complex form.
2011 Trailing zeroes matter for decimal float constants, so don't return
2012 1 for them. */
2015 real_onep (const_tree expr)
2017 STRIP_NOPS (expr);
2019 switch (TREE_CODE (expr))
2021 case REAL_CST:
2022 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2023 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2024 case COMPLEX_CST:
2025 return real_onep (TREE_REALPART (expr))
2026 && real_zerop (TREE_IMAGPART (expr));
2027 case VECTOR_CST:
2029 unsigned i;
2030 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2031 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2032 return false;
2033 return true;
2035 default:
2036 return false;
2040 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2041 for decimal float constants, so don't return 1 for them. */
2044 real_twop (const_tree expr)
2046 STRIP_NOPS (expr);
2048 switch (TREE_CODE (expr))
2050 case REAL_CST:
2051 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2052 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2053 case COMPLEX_CST:
2054 return real_twop (TREE_REALPART (expr))
2055 && real_zerop (TREE_IMAGPART (expr));
2056 case VECTOR_CST:
2058 unsigned i;
2059 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2060 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2061 return false;
2062 return true;
2064 default:
2065 return false;
2069 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2070 matter for decimal float constants, so don't return 1 for them. */
2073 real_minus_onep (const_tree expr)
2075 STRIP_NOPS (expr);
2077 switch (TREE_CODE (expr))
2079 case REAL_CST:
2080 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2081 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2082 case COMPLEX_CST:
2083 return real_minus_onep (TREE_REALPART (expr))
2084 && real_zerop (TREE_IMAGPART (expr));
2085 case VECTOR_CST:
2087 unsigned i;
2088 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2089 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2090 return false;
2091 return true;
2093 default:
2094 return false;
2098 /* Nonzero if EXP is a constant or a cast of a constant. */
2101 really_constant_p (const_tree exp)
2103 /* This is not quite the same as STRIP_NOPS. It does more. */
2104 while (CONVERT_EXPR_P (exp)
2105 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2106 exp = TREE_OPERAND (exp, 0);
2107 return TREE_CONSTANT (exp);
2110 /* Return first list element whose TREE_VALUE is ELEM.
2111 Return 0 if ELEM is not in LIST. */
2113 tree
2114 value_member (tree elem, tree list)
2116 while (list)
2118 if (elem == TREE_VALUE (list))
2119 return list;
2120 list = TREE_CHAIN (list);
2122 return NULL_TREE;
2125 /* Return first list element whose TREE_PURPOSE is ELEM.
2126 Return 0 if ELEM is not in LIST. */
2128 tree
2129 purpose_member (const_tree elem, tree list)
2131 while (list)
2133 if (elem == TREE_PURPOSE (list))
2134 return list;
2135 list = TREE_CHAIN (list);
2137 return NULL_TREE;
2140 /* Return true if ELEM is in V. */
2142 bool
2143 vec_member (const_tree elem, vec<tree, va_gc> *v)
2145 unsigned ix;
2146 tree t;
2147 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2148 if (elem == t)
2149 return true;
2150 return false;
2153 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2154 NULL_TREE. */
2156 tree
2157 chain_index (int idx, tree chain)
2159 for (; chain && idx > 0; --idx)
2160 chain = TREE_CHAIN (chain);
2161 return chain;
2164 /* Return nonzero if ELEM is part of the chain CHAIN. */
2167 chain_member (const_tree elem, const_tree chain)
2169 while (chain)
2171 if (elem == chain)
2172 return 1;
2173 chain = DECL_CHAIN (chain);
2176 return 0;
2179 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2180 We expect a null pointer to mark the end of the chain.
2181 This is the Lisp primitive `length'. */
2184 list_length (const_tree t)
2186 const_tree p = t;
2187 #ifdef ENABLE_TREE_CHECKING
2188 const_tree q = t;
2189 #endif
2190 int len = 0;
2192 while (p)
2194 p = TREE_CHAIN (p);
2195 #ifdef ENABLE_TREE_CHECKING
2196 if (len % 2)
2197 q = TREE_CHAIN (q);
2198 gcc_assert (p != q);
2199 #endif
2200 len++;
2203 return len;
2206 /* Returns the number of FIELD_DECLs in TYPE. */
2209 fields_length (const_tree type)
2211 tree t = TYPE_FIELDS (type);
2212 int count = 0;
2214 for (; t; t = DECL_CHAIN (t))
2215 if (TREE_CODE (t) == FIELD_DECL)
2216 ++count;
2218 return count;
2221 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2222 UNION_TYPE TYPE, or NULL_TREE if none. */
2224 tree
2225 first_field (const_tree type)
2227 tree t = TYPE_FIELDS (type);
2228 while (t && TREE_CODE (t) != FIELD_DECL)
2229 t = TREE_CHAIN (t);
2230 return t;
2233 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2234 by modifying the last node in chain 1 to point to chain 2.
2235 This is the Lisp primitive `nconc'. */
2237 tree
2238 chainon (tree op1, tree op2)
2240 tree t1;
2242 if (!op1)
2243 return op2;
2244 if (!op2)
2245 return op1;
2247 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2248 continue;
2249 TREE_CHAIN (t1) = op2;
2251 #ifdef ENABLE_TREE_CHECKING
2253 tree t2;
2254 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2255 gcc_assert (t2 != t1);
2257 #endif
2259 return op1;
2262 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2264 tree
2265 tree_last (tree chain)
2267 tree next;
2268 if (chain)
2269 while ((next = TREE_CHAIN (chain)))
2270 chain = next;
2271 return chain;
2274 /* Reverse the order of elements in the chain T,
2275 and return the new head of the chain (old last element). */
2277 tree
2278 nreverse (tree t)
2280 tree prev = 0, decl, next;
2281 for (decl = t; decl; decl = next)
2283 /* We shouldn't be using this function to reverse BLOCK chains; we
2284 have blocks_nreverse for that. */
2285 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2286 next = TREE_CHAIN (decl);
2287 TREE_CHAIN (decl) = prev;
2288 prev = decl;
2290 return prev;
2293 /* Return a newly created TREE_LIST node whose
2294 purpose and value fields are PARM and VALUE. */
2296 tree
2297 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2299 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2300 TREE_PURPOSE (t) = parm;
2301 TREE_VALUE (t) = value;
2302 return t;
2305 /* Build a chain of TREE_LIST nodes from a vector. */
2307 tree
2308 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2310 tree ret = NULL_TREE;
2311 tree *pp = &ret;
2312 unsigned int i;
2313 tree t;
2314 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2316 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2317 pp = &TREE_CHAIN (*pp);
2319 return ret;
2322 /* Return a newly created TREE_LIST node whose
2323 purpose and value fields are PURPOSE and VALUE
2324 and whose TREE_CHAIN is CHAIN. */
2326 tree
2327 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2329 tree node;
2331 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2332 memset (node, 0, sizeof (struct tree_common));
2334 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2336 TREE_SET_CODE (node, TREE_LIST);
2337 TREE_CHAIN (node) = chain;
2338 TREE_PURPOSE (node) = purpose;
2339 TREE_VALUE (node) = value;
2340 return node;
2343 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2344 trees. */
2346 vec<tree, va_gc> *
2347 ctor_to_vec (tree ctor)
2349 vec<tree, va_gc> *vec;
2350 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2351 unsigned int ix;
2352 tree val;
2354 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2355 vec->quick_push (val);
2357 return vec;
2360 /* Return the size nominally occupied by an object of type TYPE
2361 when it resides in memory. The value is measured in units of bytes,
2362 and its data type is that normally used for type sizes
2363 (which is the first type created by make_signed_type or
2364 make_unsigned_type). */
2366 tree
2367 size_in_bytes (const_tree type)
2369 tree t;
2371 if (type == error_mark_node)
2372 return integer_zero_node;
2374 type = TYPE_MAIN_VARIANT (type);
2375 t = TYPE_SIZE_UNIT (type);
2377 if (t == 0)
2379 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2380 return size_zero_node;
2383 return t;
2386 /* Return the size of TYPE (in bytes) as a wide integer
2387 or return -1 if the size can vary or is larger than an integer. */
2389 HOST_WIDE_INT
2390 int_size_in_bytes (const_tree type)
2392 tree t;
2394 if (type == error_mark_node)
2395 return 0;
2397 type = TYPE_MAIN_VARIANT (type);
2398 t = TYPE_SIZE_UNIT (type);
2399 if (t == 0
2400 || TREE_CODE (t) != INTEGER_CST
2401 || TREE_INT_CST_HIGH (t) != 0
2402 /* If the result would appear negative, it's too big to represent. */
2403 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2404 return -1;
2406 return TREE_INT_CST_LOW (t);
2409 /* Return the maximum size of TYPE (in bytes) as a wide integer
2410 or return -1 if the size can vary or is larger than an integer. */
2412 HOST_WIDE_INT
2413 max_int_size_in_bytes (const_tree type)
2415 HOST_WIDE_INT size = -1;
2416 tree size_tree;
2418 /* If this is an array type, check for a possible MAX_SIZE attached. */
2420 if (TREE_CODE (type) == ARRAY_TYPE)
2422 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2424 if (size_tree && host_integerp (size_tree, 1))
2425 size = tree_low_cst (size_tree, 1);
2428 /* If we still haven't been able to get a size, see if the language
2429 can compute a maximum size. */
2431 if (size == -1)
2433 size_tree = lang_hooks.types.max_size (type);
2435 if (size_tree && host_integerp (size_tree, 1))
2436 size = tree_low_cst (size_tree, 1);
2439 return size;
2442 /* Returns a tree for the size of EXP in bytes. */
2444 tree
2445 tree_expr_size (const_tree exp)
2447 if (DECL_P (exp)
2448 && DECL_SIZE_UNIT (exp) != 0)
2449 return DECL_SIZE_UNIT (exp);
2450 else
2451 return size_in_bytes (TREE_TYPE (exp));
2454 /* Return the bit position of FIELD, in bits from the start of the record.
2455 This is a tree of type bitsizetype. */
2457 tree
2458 bit_position (const_tree field)
2460 return bit_from_pos (DECL_FIELD_OFFSET (field),
2461 DECL_FIELD_BIT_OFFSET (field));
2464 /* Likewise, but return as an integer. It must be representable in
2465 that way (since it could be a signed value, we don't have the
2466 option of returning -1 like int_size_in_byte can. */
2468 HOST_WIDE_INT
2469 int_bit_position (const_tree field)
2471 return tree_low_cst (bit_position (field), 0);
2474 /* Return the byte position of FIELD, in bytes from the start of the record.
2475 This is a tree of type sizetype. */
2477 tree
2478 byte_position (const_tree field)
2480 return byte_from_pos (DECL_FIELD_OFFSET (field),
2481 DECL_FIELD_BIT_OFFSET (field));
2484 /* Likewise, but return as an integer. It must be representable in
2485 that way (since it could be a signed value, we don't have the
2486 option of returning -1 like int_size_in_byte can. */
2488 HOST_WIDE_INT
2489 int_byte_position (const_tree field)
2491 return tree_low_cst (byte_position (field), 0);
2494 /* Return the strictest alignment, in bits, that T is known to have. */
2496 unsigned int
2497 expr_align (const_tree t)
2499 unsigned int align0, align1;
2501 switch (TREE_CODE (t))
2503 CASE_CONVERT: case NON_LVALUE_EXPR:
2504 /* If we have conversions, we know that the alignment of the
2505 object must meet each of the alignments of the types. */
2506 align0 = expr_align (TREE_OPERAND (t, 0));
2507 align1 = TYPE_ALIGN (TREE_TYPE (t));
2508 return MAX (align0, align1);
2510 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2511 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2512 case CLEANUP_POINT_EXPR:
2513 /* These don't change the alignment of an object. */
2514 return expr_align (TREE_OPERAND (t, 0));
2516 case COND_EXPR:
2517 /* The best we can do is say that the alignment is the least aligned
2518 of the two arms. */
2519 align0 = expr_align (TREE_OPERAND (t, 1));
2520 align1 = expr_align (TREE_OPERAND (t, 2));
2521 return MIN (align0, align1);
2523 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2524 meaningfully, it's always 1. */
2525 case LABEL_DECL: case CONST_DECL:
2526 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2527 case FUNCTION_DECL:
2528 gcc_assert (DECL_ALIGN (t) != 0);
2529 return DECL_ALIGN (t);
2531 default:
2532 break;
2535 /* Otherwise take the alignment from that of the type. */
2536 return TYPE_ALIGN (TREE_TYPE (t));
2539 /* Return, as a tree node, the number of elements for TYPE (which is an
2540 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2542 tree
2543 array_type_nelts (const_tree type)
2545 tree index_type, min, max;
2547 /* If they did it with unspecified bounds, then we should have already
2548 given an error about it before we got here. */
2549 if (! TYPE_DOMAIN (type))
2550 return error_mark_node;
2552 index_type = TYPE_DOMAIN (type);
2553 min = TYPE_MIN_VALUE (index_type);
2554 max = TYPE_MAX_VALUE (index_type);
2556 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2557 if (!max)
2558 return error_mark_node;
2560 return (integer_zerop (min)
2561 ? max
2562 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2565 /* If arg is static -- a reference to an object in static storage -- then
2566 return the object. This is not the same as the C meaning of `static'.
2567 If arg isn't static, return NULL. */
2569 tree
2570 staticp (tree arg)
2572 switch (TREE_CODE (arg))
2574 case FUNCTION_DECL:
2575 /* Nested functions are static, even though taking their address will
2576 involve a trampoline as we unnest the nested function and create
2577 the trampoline on the tree level. */
2578 return arg;
2580 case VAR_DECL:
2581 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2582 && ! DECL_THREAD_LOCAL_P (arg)
2583 && ! DECL_DLLIMPORT_P (arg)
2584 ? arg : NULL);
2586 case CONST_DECL:
2587 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2588 ? arg : NULL);
2590 case CONSTRUCTOR:
2591 return TREE_STATIC (arg) ? arg : NULL;
2593 case LABEL_DECL:
2594 case STRING_CST:
2595 return arg;
2597 case COMPONENT_REF:
2598 /* If the thing being referenced is not a field, then it is
2599 something language specific. */
2600 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2602 /* If we are referencing a bitfield, we can't evaluate an
2603 ADDR_EXPR at compile time and so it isn't a constant. */
2604 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2605 return NULL;
2607 return staticp (TREE_OPERAND (arg, 0));
2609 case BIT_FIELD_REF:
2610 return NULL;
2612 case INDIRECT_REF:
2613 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2615 case ARRAY_REF:
2616 case ARRAY_RANGE_REF:
2617 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2618 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2619 return staticp (TREE_OPERAND (arg, 0));
2620 else
2621 return NULL;
2623 case COMPOUND_LITERAL_EXPR:
2624 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2626 default:
2627 return NULL;
2634 /* Return whether OP is a DECL whose address is function-invariant. */
2636 bool
2637 decl_address_invariant_p (const_tree op)
2639 /* The conditions below are slightly less strict than the one in
2640 staticp. */
2642 switch (TREE_CODE (op))
2644 case PARM_DECL:
2645 case RESULT_DECL:
2646 case LABEL_DECL:
2647 case FUNCTION_DECL:
2648 return true;
2650 case VAR_DECL:
2651 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2652 || DECL_THREAD_LOCAL_P (op)
2653 || DECL_CONTEXT (op) == current_function_decl
2654 || decl_function_context (op) == current_function_decl)
2655 return true;
2656 break;
2658 case CONST_DECL:
2659 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2660 || decl_function_context (op) == current_function_decl)
2661 return true;
2662 break;
2664 default:
2665 break;
2668 return false;
2671 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2673 bool
2674 decl_address_ip_invariant_p (const_tree op)
2676 /* The conditions below are slightly less strict than the one in
2677 staticp. */
2679 switch (TREE_CODE (op))
2681 case LABEL_DECL:
2682 case FUNCTION_DECL:
2683 case STRING_CST:
2684 return true;
2686 case VAR_DECL:
2687 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2688 && !DECL_DLLIMPORT_P (op))
2689 || DECL_THREAD_LOCAL_P (op))
2690 return true;
2691 break;
2693 case CONST_DECL:
2694 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2695 return true;
2696 break;
2698 default:
2699 break;
2702 return false;
2706 /* Return true if T is function-invariant (internal function, does
2707 not handle arithmetic; that's handled in skip_simple_arithmetic and
2708 tree_invariant_p). */
2710 static bool tree_invariant_p (tree t);
2712 static bool
2713 tree_invariant_p_1 (tree t)
2715 tree op;
2717 if (TREE_CONSTANT (t)
2718 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2719 return true;
2721 switch (TREE_CODE (t))
2723 case SAVE_EXPR:
2724 return true;
2726 case ADDR_EXPR:
2727 op = TREE_OPERAND (t, 0);
2728 while (handled_component_p (op))
2730 switch (TREE_CODE (op))
2732 case ARRAY_REF:
2733 case ARRAY_RANGE_REF:
2734 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2735 || TREE_OPERAND (op, 2) != NULL_TREE
2736 || TREE_OPERAND (op, 3) != NULL_TREE)
2737 return false;
2738 break;
2740 case COMPONENT_REF:
2741 if (TREE_OPERAND (op, 2) != NULL_TREE)
2742 return false;
2743 break;
2745 default:;
2747 op = TREE_OPERAND (op, 0);
2750 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2752 default:
2753 break;
2756 return false;
2759 /* Return true if T is function-invariant. */
2761 static bool
2762 tree_invariant_p (tree t)
2764 tree inner = skip_simple_arithmetic (t);
2765 return tree_invariant_p_1 (inner);
2768 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2769 Do this to any expression which may be used in more than one place,
2770 but must be evaluated only once.
2772 Normally, expand_expr would reevaluate the expression each time.
2773 Calling save_expr produces something that is evaluated and recorded
2774 the first time expand_expr is called on it. Subsequent calls to
2775 expand_expr just reuse the recorded value.
2777 The call to expand_expr that generates code that actually computes
2778 the value is the first call *at compile time*. Subsequent calls
2779 *at compile time* generate code to use the saved value.
2780 This produces correct result provided that *at run time* control
2781 always flows through the insns made by the first expand_expr
2782 before reaching the other places where the save_expr was evaluated.
2783 You, the caller of save_expr, must make sure this is so.
2785 Constants, and certain read-only nodes, are returned with no
2786 SAVE_EXPR because that is safe. Expressions containing placeholders
2787 are not touched; see tree.def for an explanation of what these
2788 are used for. */
2790 tree
2791 save_expr (tree expr)
2793 tree t = fold (expr);
2794 tree inner;
2796 /* If the tree evaluates to a constant, then we don't want to hide that
2797 fact (i.e. this allows further folding, and direct checks for constants).
2798 However, a read-only object that has side effects cannot be bypassed.
2799 Since it is no problem to reevaluate literals, we just return the
2800 literal node. */
2801 inner = skip_simple_arithmetic (t);
2802 if (TREE_CODE (inner) == ERROR_MARK)
2803 return inner;
2805 if (tree_invariant_p_1 (inner))
2806 return t;
2808 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2809 it means that the size or offset of some field of an object depends on
2810 the value within another field.
2812 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2813 and some variable since it would then need to be both evaluated once and
2814 evaluated more than once. Front-ends must assure this case cannot
2815 happen by surrounding any such subexpressions in their own SAVE_EXPR
2816 and forcing evaluation at the proper time. */
2817 if (contains_placeholder_p (inner))
2818 return t;
2820 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2821 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2823 /* This expression might be placed ahead of a jump to ensure that the
2824 value was computed on both sides of the jump. So make sure it isn't
2825 eliminated as dead. */
2826 TREE_SIDE_EFFECTS (t) = 1;
2827 return t;
2830 /* Look inside EXPR and into any simple arithmetic operations. Return
2831 the innermost non-arithmetic node. */
2833 tree
2834 skip_simple_arithmetic (tree expr)
2836 tree inner;
2838 /* We don't care about whether this can be used as an lvalue in this
2839 context. */
2840 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2841 expr = TREE_OPERAND (expr, 0);
2843 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2844 a constant, it will be more efficient to not make another SAVE_EXPR since
2845 it will allow better simplification and GCSE will be able to merge the
2846 computations if they actually occur. */
2847 inner = expr;
2848 while (1)
2850 if (UNARY_CLASS_P (inner))
2851 inner = TREE_OPERAND (inner, 0);
2852 else if (BINARY_CLASS_P (inner))
2854 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2855 inner = TREE_OPERAND (inner, 0);
2856 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2857 inner = TREE_OPERAND (inner, 1);
2858 else
2859 break;
2861 else
2862 break;
2865 return inner;
2869 /* Return which tree structure is used by T. */
2871 enum tree_node_structure_enum
2872 tree_node_structure (const_tree t)
2874 const enum tree_code code = TREE_CODE (t);
2875 return tree_node_structure_for_code (code);
2878 /* Set various status flags when building a CALL_EXPR object T. */
2880 static void
2881 process_call_operands (tree t)
2883 bool side_effects = TREE_SIDE_EFFECTS (t);
2884 bool read_only = false;
2885 int i = call_expr_flags (t);
2887 /* Calls have side-effects, except those to const or pure functions. */
2888 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2889 side_effects = true;
2890 /* Propagate TREE_READONLY of arguments for const functions. */
2891 if (i & ECF_CONST)
2892 read_only = true;
2894 if (!side_effects || read_only)
2895 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2897 tree op = TREE_OPERAND (t, i);
2898 if (op && TREE_SIDE_EFFECTS (op))
2899 side_effects = true;
2900 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2901 read_only = false;
2904 TREE_SIDE_EFFECTS (t) = side_effects;
2905 TREE_READONLY (t) = read_only;
2908 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
2909 size or offset that depends on a field within a record. */
2911 bool
2912 contains_placeholder_p (const_tree exp)
2914 enum tree_code code;
2916 if (!exp)
2917 return 0;
2919 code = TREE_CODE (exp);
2920 if (code == PLACEHOLDER_EXPR)
2921 return 1;
2923 switch (TREE_CODE_CLASS (code))
2925 case tcc_reference:
2926 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2927 position computations since they will be converted into a
2928 WITH_RECORD_EXPR involving the reference, which will assume
2929 here will be valid. */
2930 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2932 case tcc_exceptional:
2933 if (code == TREE_LIST)
2934 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2935 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2936 break;
2938 case tcc_unary:
2939 case tcc_binary:
2940 case tcc_comparison:
2941 case tcc_expression:
2942 switch (code)
2944 case COMPOUND_EXPR:
2945 /* Ignoring the first operand isn't quite right, but works best. */
2946 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2948 case COND_EXPR:
2949 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2950 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2951 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2953 case SAVE_EXPR:
2954 /* The save_expr function never wraps anything containing
2955 a PLACEHOLDER_EXPR. */
2956 return 0;
2958 default:
2959 break;
2962 switch (TREE_CODE_LENGTH (code))
2964 case 1:
2965 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2966 case 2:
2967 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2968 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2969 default:
2970 return 0;
2973 case tcc_vl_exp:
2974 switch (code)
2976 case CALL_EXPR:
2978 const_tree arg;
2979 const_call_expr_arg_iterator iter;
2980 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2981 if (CONTAINS_PLACEHOLDER_P (arg))
2982 return 1;
2983 return 0;
2985 default:
2986 return 0;
2989 default:
2990 return 0;
2992 return 0;
2995 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
2996 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
2997 field positions. */
2999 static bool
3000 type_contains_placeholder_1 (const_tree type)
3002 /* If the size contains a placeholder or the parent type (component type in
3003 the case of arrays) type involves a placeholder, this type does. */
3004 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3005 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3006 || (!POINTER_TYPE_P (type)
3007 && TREE_TYPE (type)
3008 && type_contains_placeholder_p (TREE_TYPE (type))))
3009 return true;
3011 /* Now do type-specific checks. Note that the last part of the check above
3012 greatly limits what we have to do below. */
3013 switch (TREE_CODE (type))
3015 case VOID_TYPE:
3016 case COMPLEX_TYPE:
3017 case ENUMERAL_TYPE:
3018 case BOOLEAN_TYPE:
3019 case POINTER_TYPE:
3020 case OFFSET_TYPE:
3021 case REFERENCE_TYPE:
3022 case METHOD_TYPE:
3023 case FUNCTION_TYPE:
3024 case VECTOR_TYPE:
3025 case NULLPTR_TYPE:
3026 return false;
3028 case INTEGER_TYPE:
3029 case REAL_TYPE:
3030 case FIXED_POINT_TYPE:
3031 /* Here we just check the bounds. */
3032 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3033 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3035 case ARRAY_TYPE:
3036 /* We have already checked the component type above, so just check the
3037 domain type. */
3038 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3040 case RECORD_TYPE:
3041 case UNION_TYPE:
3042 case QUAL_UNION_TYPE:
3044 tree field;
3046 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3047 if (TREE_CODE (field) == FIELD_DECL
3048 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3049 || (TREE_CODE (type) == QUAL_UNION_TYPE
3050 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3051 || type_contains_placeholder_p (TREE_TYPE (field))))
3052 return true;
3054 return false;
3057 default:
3058 gcc_unreachable ();
3062 /* Wrapper around above function used to cache its result. */
3064 bool
3065 type_contains_placeholder_p (tree type)
3067 bool result;
3069 /* If the contains_placeholder_bits field has been initialized,
3070 then we know the answer. */
3071 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3072 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3074 /* Indicate that we've seen this type node, and the answer is false.
3075 This is what we want to return if we run into recursion via fields. */
3076 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3078 /* Compute the real value. */
3079 result = type_contains_placeholder_1 (type);
3081 /* Store the real value. */
3082 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3084 return result;
3087 /* Push tree EXP onto vector QUEUE if it is not already present. */
3089 static void
3090 push_without_duplicates (tree exp, vec<tree> *queue)
3092 unsigned int i;
3093 tree iter;
3095 FOR_EACH_VEC_ELT (*queue, i, iter)
3096 if (simple_cst_equal (iter, exp) == 1)
3097 break;
3099 if (!iter)
3100 queue->safe_push (exp);
3103 /* Given a tree EXP, find all occurrences of references to fields
3104 in a PLACEHOLDER_EXPR and place them in vector REFS without
3105 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3106 we assume here that EXP contains only arithmetic expressions
3107 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3108 argument list. */
3110 void
3111 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3113 enum tree_code code = TREE_CODE (exp);
3114 tree inner;
3115 int i;
3117 /* We handle TREE_LIST and COMPONENT_REF separately. */
3118 if (code == TREE_LIST)
3120 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3121 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3123 else if (code == COMPONENT_REF)
3125 for (inner = TREE_OPERAND (exp, 0);
3126 REFERENCE_CLASS_P (inner);
3127 inner = TREE_OPERAND (inner, 0))
3130 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3131 push_without_duplicates (exp, refs);
3132 else
3133 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3135 else
3136 switch (TREE_CODE_CLASS (code))
3138 case tcc_constant:
3139 break;
3141 case tcc_declaration:
3142 /* Variables allocated to static storage can stay. */
3143 if (!TREE_STATIC (exp))
3144 push_without_duplicates (exp, refs);
3145 break;
3147 case tcc_expression:
3148 /* This is the pattern built in ada/make_aligning_type. */
3149 if (code == ADDR_EXPR
3150 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3152 push_without_duplicates (exp, refs);
3153 break;
3156 /* Fall through... */
3158 case tcc_exceptional:
3159 case tcc_unary:
3160 case tcc_binary:
3161 case tcc_comparison:
3162 case tcc_reference:
3163 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3164 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3165 break;
3167 case tcc_vl_exp:
3168 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3169 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3170 break;
3172 default:
3173 gcc_unreachable ();
3177 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3178 return a tree with all occurrences of references to F in a
3179 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3180 CONST_DECLs. Note that we assume here that EXP contains only
3181 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3182 occurring only in their argument list. */
3184 tree
3185 substitute_in_expr (tree exp, tree f, tree r)
3187 enum tree_code code = TREE_CODE (exp);
3188 tree op0, op1, op2, op3;
3189 tree new_tree;
3191 /* We handle TREE_LIST and COMPONENT_REF separately. */
3192 if (code == TREE_LIST)
3194 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3195 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3196 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3197 return exp;
3199 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3201 else if (code == COMPONENT_REF)
3203 tree inner;
3205 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3206 and it is the right field, replace it with R. */
3207 for (inner = TREE_OPERAND (exp, 0);
3208 REFERENCE_CLASS_P (inner);
3209 inner = TREE_OPERAND (inner, 0))
3212 /* The field. */
3213 op1 = TREE_OPERAND (exp, 1);
3215 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3216 return r;
3218 /* If this expression hasn't been completed let, leave it alone. */
3219 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3220 return exp;
3222 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3223 if (op0 == TREE_OPERAND (exp, 0))
3224 return exp;
3226 new_tree
3227 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3229 else
3230 switch (TREE_CODE_CLASS (code))
3232 case tcc_constant:
3233 return exp;
3235 case tcc_declaration:
3236 if (exp == f)
3237 return r;
3238 else
3239 return exp;
3241 case tcc_expression:
3242 if (exp == f)
3243 return r;
3245 /* Fall through... */
3247 case tcc_exceptional:
3248 case tcc_unary:
3249 case tcc_binary:
3250 case tcc_comparison:
3251 case tcc_reference:
3252 switch (TREE_CODE_LENGTH (code))
3254 case 0:
3255 return exp;
3257 case 1:
3258 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3259 if (op0 == TREE_OPERAND (exp, 0))
3260 return exp;
3262 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3263 break;
3265 case 2:
3266 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3267 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3269 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3270 return exp;
3272 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3273 break;
3275 case 3:
3276 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3277 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3278 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3280 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3281 && op2 == TREE_OPERAND (exp, 2))
3282 return exp;
3284 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3285 break;
3287 case 4:
3288 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3289 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3290 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3291 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3293 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3294 && op2 == TREE_OPERAND (exp, 2)
3295 && op3 == TREE_OPERAND (exp, 3))
3296 return exp;
3298 new_tree
3299 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3300 break;
3302 default:
3303 gcc_unreachable ();
3305 break;
3307 case tcc_vl_exp:
3309 int i;
3311 new_tree = NULL_TREE;
3313 /* If we are trying to replace F with a constant, inline back
3314 functions which do nothing else than computing a value from
3315 the arguments they are passed. This makes it possible to
3316 fold partially or entirely the replacement expression. */
3317 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3319 tree t = maybe_inline_call_in_expr (exp);
3320 if (t)
3321 return SUBSTITUTE_IN_EXPR (t, f, r);
3324 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3326 tree op = TREE_OPERAND (exp, i);
3327 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3328 if (new_op != op)
3330 if (!new_tree)
3331 new_tree = copy_node (exp);
3332 TREE_OPERAND (new_tree, i) = new_op;
3336 if (new_tree)
3338 new_tree = fold (new_tree);
3339 if (TREE_CODE (new_tree) == CALL_EXPR)
3340 process_call_operands (new_tree);
3342 else
3343 return exp;
3345 break;
3347 default:
3348 gcc_unreachable ();
3351 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3353 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3354 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3356 return new_tree;
3359 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3360 for it within OBJ, a tree that is an object or a chain of references. */
3362 tree
3363 substitute_placeholder_in_expr (tree exp, tree obj)
3365 enum tree_code code = TREE_CODE (exp);
3366 tree op0, op1, op2, op3;
3367 tree new_tree;
3369 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3370 in the chain of OBJ. */
3371 if (code == PLACEHOLDER_EXPR)
3373 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3374 tree elt;
3376 for (elt = obj; elt != 0;
3377 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3378 || TREE_CODE (elt) == COND_EXPR)
3379 ? TREE_OPERAND (elt, 1)
3380 : (REFERENCE_CLASS_P (elt)
3381 || UNARY_CLASS_P (elt)
3382 || BINARY_CLASS_P (elt)
3383 || VL_EXP_CLASS_P (elt)
3384 || EXPRESSION_CLASS_P (elt))
3385 ? TREE_OPERAND (elt, 0) : 0))
3386 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3387 return elt;
3389 for (elt = obj; elt != 0;
3390 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3391 || TREE_CODE (elt) == COND_EXPR)
3392 ? TREE_OPERAND (elt, 1)
3393 : (REFERENCE_CLASS_P (elt)
3394 || UNARY_CLASS_P (elt)
3395 || BINARY_CLASS_P (elt)
3396 || VL_EXP_CLASS_P (elt)
3397 || EXPRESSION_CLASS_P (elt))
3398 ? TREE_OPERAND (elt, 0) : 0))
3399 if (POINTER_TYPE_P (TREE_TYPE (elt))
3400 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3401 == need_type))
3402 return fold_build1 (INDIRECT_REF, need_type, elt);
3404 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3405 survives until RTL generation, there will be an error. */
3406 return exp;
3409 /* TREE_LIST is special because we need to look at TREE_VALUE
3410 and TREE_CHAIN, not TREE_OPERANDS. */
3411 else if (code == TREE_LIST)
3413 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3414 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3415 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3416 return exp;
3418 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3420 else
3421 switch (TREE_CODE_CLASS (code))
3423 case tcc_constant:
3424 case tcc_declaration:
3425 return exp;
3427 case tcc_exceptional:
3428 case tcc_unary:
3429 case tcc_binary:
3430 case tcc_comparison:
3431 case tcc_expression:
3432 case tcc_reference:
3433 case tcc_statement:
3434 switch (TREE_CODE_LENGTH (code))
3436 case 0:
3437 return exp;
3439 case 1:
3440 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3441 if (op0 == TREE_OPERAND (exp, 0))
3442 return exp;
3444 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3445 break;
3447 case 2:
3448 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3449 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3451 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3452 return exp;
3454 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3455 break;
3457 case 3:
3458 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3459 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3460 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3462 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3463 && op2 == TREE_OPERAND (exp, 2))
3464 return exp;
3466 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3467 break;
3469 case 4:
3470 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3471 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3472 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3473 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3475 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3476 && op2 == TREE_OPERAND (exp, 2)
3477 && op3 == TREE_OPERAND (exp, 3))
3478 return exp;
3480 new_tree
3481 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3482 break;
3484 default:
3485 gcc_unreachable ();
3487 break;
3489 case tcc_vl_exp:
3491 int i;
3493 new_tree = NULL_TREE;
3495 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3497 tree op = TREE_OPERAND (exp, i);
3498 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3499 if (new_op != op)
3501 if (!new_tree)
3502 new_tree = copy_node (exp);
3503 TREE_OPERAND (new_tree, i) = new_op;
3507 if (new_tree)
3509 new_tree = fold (new_tree);
3510 if (TREE_CODE (new_tree) == CALL_EXPR)
3511 process_call_operands (new_tree);
3513 else
3514 return exp;
3516 break;
3518 default:
3519 gcc_unreachable ();
3522 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3524 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3525 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3527 return new_tree;
3530 /* Stabilize a reference so that we can use it any number of times
3531 without causing its operands to be evaluated more than once.
3532 Returns the stabilized reference. This works by means of save_expr,
3533 so see the caveats in the comments about save_expr.
3535 Also allows conversion expressions whose operands are references.
3536 Any other kind of expression is returned unchanged. */
3538 tree
3539 stabilize_reference (tree ref)
3541 tree result;
3542 enum tree_code code = TREE_CODE (ref);
3544 switch (code)
3546 case VAR_DECL:
3547 case PARM_DECL:
3548 case RESULT_DECL:
3549 /* No action is needed in this case. */
3550 return ref;
3552 CASE_CONVERT:
3553 case FLOAT_EXPR:
3554 case FIX_TRUNC_EXPR:
3555 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3556 break;
3558 case INDIRECT_REF:
3559 result = build_nt (INDIRECT_REF,
3560 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3561 break;
3563 case COMPONENT_REF:
3564 result = build_nt (COMPONENT_REF,
3565 stabilize_reference (TREE_OPERAND (ref, 0)),
3566 TREE_OPERAND (ref, 1), NULL_TREE);
3567 break;
3569 case BIT_FIELD_REF:
3570 result = build_nt (BIT_FIELD_REF,
3571 stabilize_reference (TREE_OPERAND (ref, 0)),
3572 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3573 break;
3575 case ARRAY_REF:
3576 result = build_nt (ARRAY_REF,
3577 stabilize_reference (TREE_OPERAND (ref, 0)),
3578 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3579 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3580 break;
3582 case ARRAY_RANGE_REF:
3583 result = build_nt (ARRAY_RANGE_REF,
3584 stabilize_reference (TREE_OPERAND (ref, 0)),
3585 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3586 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3587 break;
3589 case COMPOUND_EXPR:
3590 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3591 it wouldn't be ignored. This matters when dealing with
3592 volatiles. */
3593 return stabilize_reference_1 (ref);
3595 /* If arg isn't a kind of lvalue we recognize, make no change.
3596 Caller should recognize the error for an invalid lvalue. */
3597 default:
3598 return ref;
3600 case ERROR_MARK:
3601 return error_mark_node;
3604 TREE_TYPE (result) = TREE_TYPE (ref);
3605 TREE_READONLY (result) = TREE_READONLY (ref);
3606 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3607 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3609 return result;
3612 /* Subroutine of stabilize_reference; this is called for subtrees of
3613 references. Any expression with side-effects must be put in a SAVE_EXPR
3614 to ensure that it is only evaluated once.
3616 We don't put SAVE_EXPR nodes around everything, because assigning very
3617 simple expressions to temporaries causes us to miss good opportunities
3618 for optimizations. Among other things, the opportunity to fold in the
3619 addition of a constant into an addressing mode often gets lost, e.g.
3620 "y[i+1] += x;". In general, we take the approach that we should not make
3621 an assignment unless we are forced into it - i.e., that any non-side effect
3622 operator should be allowed, and that cse should take care of coalescing
3623 multiple utterances of the same expression should that prove fruitful. */
3625 tree
3626 stabilize_reference_1 (tree e)
3628 tree result;
3629 enum tree_code code = TREE_CODE (e);
3631 /* We cannot ignore const expressions because it might be a reference
3632 to a const array but whose index contains side-effects. But we can
3633 ignore things that are actual constant or that already have been
3634 handled by this function. */
3636 if (tree_invariant_p (e))
3637 return e;
3639 switch (TREE_CODE_CLASS (code))
3641 case tcc_exceptional:
3642 case tcc_type:
3643 case tcc_declaration:
3644 case tcc_comparison:
3645 case tcc_statement:
3646 case tcc_expression:
3647 case tcc_reference:
3648 case tcc_vl_exp:
3649 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3650 so that it will only be evaluated once. */
3651 /* The reference (r) and comparison (<) classes could be handled as
3652 below, but it is generally faster to only evaluate them once. */
3653 if (TREE_SIDE_EFFECTS (e))
3654 return save_expr (e);
3655 return e;
3657 case tcc_constant:
3658 /* Constants need no processing. In fact, we should never reach
3659 here. */
3660 return e;
3662 case tcc_binary:
3663 /* Division is slow and tends to be compiled with jumps,
3664 especially the division by powers of 2 that is often
3665 found inside of an array reference. So do it just once. */
3666 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3667 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3668 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3669 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3670 return save_expr (e);
3671 /* Recursively stabilize each operand. */
3672 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3673 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3674 break;
3676 case tcc_unary:
3677 /* Recursively stabilize each operand. */
3678 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3679 break;
3681 default:
3682 gcc_unreachable ();
3685 TREE_TYPE (result) = TREE_TYPE (e);
3686 TREE_READONLY (result) = TREE_READONLY (e);
3687 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3688 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3690 return result;
3693 /* Low-level constructors for expressions. */
3695 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3696 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3698 void
3699 recompute_tree_invariant_for_addr_expr (tree t)
3701 tree node;
3702 bool tc = true, se = false;
3704 /* We started out assuming this address is both invariant and constant, but
3705 does not have side effects. Now go down any handled components and see if
3706 any of them involve offsets that are either non-constant or non-invariant.
3707 Also check for side-effects.
3709 ??? Note that this code makes no attempt to deal with the case where
3710 taking the address of something causes a copy due to misalignment. */
3712 #define UPDATE_FLAGS(NODE) \
3713 do { tree _node = (NODE); \
3714 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3715 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3717 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3718 node = TREE_OPERAND (node, 0))
3720 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3721 array reference (probably made temporarily by the G++ front end),
3722 so ignore all the operands. */
3723 if ((TREE_CODE (node) == ARRAY_REF
3724 || TREE_CODE (node) == ARRAY_RANGE_REF)
3725 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3727 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3728 if (TREE_OPERAND (node, 2))
3729 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3730 if (TREE_OPERAND (node, 3))
3731 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3733 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3734 FIELD_DECL, apparently. The G++ front end can put something else
3735 there, at least temporarily. */
3736 else if (TREE_CODE (node) == COMPONENT_REF
3737 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3739 if (TREE_OPERAND (node, 2))
3740 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3744 node = lang_hooks.expr_to_decl (node, &tc, &se);
3746 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3747 the address, since &(*a)->b is a form of addition. If it's a constant, the
3748 address is constant too. If it's a decl, its address is constant if the
3749 decl is static. Everything else is not constant and, furthermore,
3750 taking the address of a volatile variable is not volatile. */
3751 if (TREE_CODE (node) == INDIRECT_REF
3752 || TREE_CODE (node) == MEM_REF)
3753 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3754 else if (CONSTANT_CLASS_P (node))
3756 else if (DECL_P (node))
3757 tc &= (staticp (node) != NULL_TREE);
3758 else
3760 tc = false;
3761 se |= TREE_SIDE_EFFECTS (node);
3765 TREE_CONSTANT (t) = tc;
3766 TREE_SIDE_EFFECTS (t) = se;
3767 #undef UPDATE_FLAGS
3770 /* Build an expression of code CODE, data type TYPE, and operands as
3771 specified. Expressions and reference nodes can be created this way.
3772 Constants, decls, types and misc nodes cannot be.
3774 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3775 enough for all extant tree codes. */
3777 tree
3778 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3780 tree t;
3782 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3784 t = make_node_stat (code PASS_MEM_STAT);
3785 TREE_TYPE (t) = tt;
3787 return t;
3790 tree
3791 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3793 int length = sizeof (struct tree_exp);
3794 tree t;
3796 record_node_allocation_statistics (code, length);
3798 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3800 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
3802 memset (t, 0, sizeof (struct tree_common));
3804 TREE_SET_CODE (t, code);
3806 TREE_TYPE (t) = type;
3807 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3808 TREE_OPERAND (t, 0) = node;
3809 if (node && !TYPE_P (node))
3811 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3812 TREE_READONLY (t) = TREE_READONLY (node);
3815 if (TREE_CODE_CLASS (code) == tcc_statement)
3816 TREE_SIDE_EFFECTS (t) = 1;
3817 else switch (code)
3819 case VA_ARG_EXPR:
3820 /* All of these have side-effects, no matter what their
3821 operands are. */
3822 TREE_SIDE_EFFECTS (t) = 1;
3823 TREE_READONLY (t) = 0;
3824 break;
3826 case INDIRECT_REF:
3827 /* Whether a dereference is readonly has nothing to do with whether
3828 its operand is readonly. */
3829 TREE_READONLY (t) = 0;
3830 break;
3832 case ADDR_EXPR:
3833 if (node)
3834 recompute_tree_invariant_for_addr_expr (t);
3835 break;
3837 default:
3838 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3839 && node && !TYPE_P (node)
3840 && TREE_CONSTANT (node))
3841 TREE_CONSTANT (t) = 1;
3842 if (TREE_CODE_CLASS (code) == tcc_reference
3843 && node && TREE_THIS_VOLATILE (node))
3844 TREE_THIS_VOLATILE (t) = 1;
3845 break;
3848 return t;
3851 #define PROCESS_ARG(N) \
3852 do { \
3853 TREE_OPERAND (t, N) = arg##N; \
3854 if (arg##N &&!TYPE_P (arg##N)) \
3856 if (TREE_SIDE_EFFECTS (arg##N)) \
3857 side_effects = 1; \
3858 if (!TREE_READONLY (arg##N) \
3859 && !CONSTANT_CLASS_P (arg##N)) \
3860 (void) (read_only = 0); \
3861 if (!TREE_CONSTANT (arg##N)) \
3862 (void) (constant = 0); \
3864 } while (0)
3866 tree
3867 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3869 bool constant, read_only, side_effects;
3870 tree t;
3872 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3874 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3875 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3876 /* When sizetype precision doesn't match that of pointers
3877 we need to be able to build explicit extensions or truncations
3878 of the offset argument. */
3879 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3880 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3881 && TREE_CODE (arg1) == INTEGER_CST);
3883 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3884 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3885 && ptrofftype_p (TREE_TYPE (arg1)));
3887 t = make_node_stat (code PASS_MEM_STAT);
3888 TREE_TYPE (t) = tt;
3890 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3891 result based on those same flags for the arguments. But if the
3892 arguments aren't really even `tree' expressions, we shouldn't be trying
3893 to do this. */
3895 /* Expressions without side effects may be constant if their
3896 arguments are as well. */
3897 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3898 || TREE_CODE_CLASS (code) == tcc_binary);
3899 read_only = 1;
3900 side_effects = TREE_SIDE_EFFECTS (t);
3902 PROCESS_ARG(0);
3903 PROCESS_ARG(1);
3905 TREE_READONLY (t) = read_only;
3906 TREE_CONSTANT (t) = constant;
3907 TREE_SIDE_EFFECTS (t) = side_effects;
3908 TREE_THIS_VOLATILE (t)
3909 = (TREE_CODE_CLASS (code) == tcc_reference
3910 && arg0 && TREE_THIS_VOLATILE (arg0));
3912 return t;
3916 tree
3917 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3918 tree arg2 MEM_STAT_DECL)
3920 bool constant, read_only, side_effects;
3921 tree t;
3923 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3924 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3926 t = make_node_stat (code PASS_MEM_STAT);
3927 TREE_TYPE (t) = tt;
3929 read_only = 1;
3931 /* As a special exception, if COND_EXPR has NULL branches, we
3932 assume that it is a gimple statement and always consider
3933 it to have side effects. */
3934 if (code == COND_EXPR
3935 && tt == void_type_node
3936 && arg1 == NULL_TREE
3937 && arg2 == NULL_TREE)
3938 side_effects = true;
3939 else
3940 side_effects = TREE_SIDE_EFFECTS (t);
3942 PROCESS_ARG(0);
3943 PROCESS_ARG(1);
3944 PROCESS_ARG(2);
3946 if (code == COND_EXPR)
3947 TREE_READONLY (t) = read_only;
3949 TREE_SIDE_EFFECTS (t) = side_effects;
3950 TREE_THIS_VOLATILE (t)
3951 = (TREE_CODE_CLASS (code) == tcc_reference
3952 && arg0 && TREE_THIS_VOLATILE (arg0));
3954 return t;
3957 tree
3958 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3959 tree arg2, tree arg3 MEM_STAT_DECL)
3961 bool constant, read_only, side_effects;
3962 tree t;
3964 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3966 t = make_node_stat (code PASS_MEM_STAT);
3967 TREE_TYPE (t) = tt;
3969 side_effects = TREE_SIDE_EFFECTS (t);
3971 PROCESS_ARG(0);
3972 PROCESS_ARG(1);
3973 PROCESS_ARG(2);
3974 PROCESS_ARG(3);
3976 TREE_SIDE_EFFECTS (t) = side_effects;
3977 TREE_THIS_VOLATILE (t)
3978 = (TREE_CODE_CLASS (code) == tcc_reference
3979 && arg0 && TREE_THIS_VOLATILE (arg0));
3981 return t;
3984 tree
3985 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3986 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3988 bool constant, read_only, side_effects;
3989 tree t;
3991 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3993 t = make_node_stat (code PASS_MEM_STAT);
3994 TREE_TYPE (t) = tt;
3996 side_effects = TREE_SIDE_EFFECTS (t);
3998 PROCESS_ARG(0);
3999 PROCESS_ARG(1);
4000 PROCESS_ARG(2);
4001 PROCESS_ARG(3);
4002 PROCESS_ARG(4);
4004 TREE_SIDE_EFFECTS (t) = side_effects;
4005 TREE_THIS_VOLATILE (t)
4006 = (TREE_CODE_CLASS (code) == tcc_reference
4007 && arg0 && TREE_THIS_VOLATILE (arg0));
4009 return t;
4012 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4013 on the pointer PTR. */
4015 tree
4016 build_simple_mem_ref_loc (location_t loc, tree ptr)
4018 HOST_WIDE_INT offset = 0;
4019 tree ptype = TREE_TYPE (ptr);
4020 tree tem;
4021 /* For convenience allow addresses that collapse to a simple base
4022 and offset. */
4023 if (TREE_CODE (ptr) == ADDR_EXPR
4024 && (handled_component_p (TREE_OPERAND (ptr, 0))
4025 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4027 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4028 gcc_assert (ptr);
4029 ptr = build_fold_addr_expr (ptr);
4030 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4032 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4033 ptr, build_int_cst (ptype, offset));
4034 SET_EXPR_LOCATION (tem, loc);
4035 return tem;
4038 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4040 double_int
4041 mem_ref_offset (const_tree t)
4043 tree toff = TREE_OPERAND (t, 1);
4044 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4047 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4048 offsetted by OFFSET units. */
4050 tree
4051 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4053 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4054 build_fold_addr_expr (base),
4055 build_int_cst (ptr_type_node, offset));
4056 tree addr = build1 (ADDR_EXPR, type, ref);
4057 recompute_tree_invariant_for_addr_expr (addr);
4058 return addr;
4061 /* Similar except don't specify the TREE_TYPE
4062 and leave the TREE_SIDE_EFFECTS as 0.
4063 It is permissible for arguments to be null,
4064 or even garbage if their values do not matter. */
4066 tree
4067 build_nt (enum tree_code code, ...)
4069 tree t;
4070 int length;
4071 int i;
4072 va_list p;
4074 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4076 va_start (p, code);
4078 t = make_node (code);
4079 length = TREE_CODE_LENGTH (code);
4081 for (i = 0; i < length; i++)
4082 TREE_OPERAND (t, i) = va_arg (p, tree);
4084 va_end (p);
4085 return t;
4088 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4089 tree vec. */
4091 tree
4092 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4094 tree ret, t;
4095 unsigned int ix;
4097 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4098 CALL_EXPR_FN (ret) = fn;
4099 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4100 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4101 CALL_EXPR_ARG (ret, ix) = t;
4102 return ret;
4105 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4106 We do NOT enter this node in any sort of symbol table.
4108 LOC is the location of the decl.
4110 layout_decl is used to set up the decl's storage layout.
4111 Other slots are initialized to 0 or null pointers. */
4113 tree
4114 build_decl_stat (location_t loc, enum tree_code code, tree name,
4115 tree type MEM_STAT_DECL)
4117 tree t;
4119 t = make_node_stat (code PASS_MEM_STAT);
4120 DECL_SOURCE_LOCATION (t) = loc;
4122 /* if (type == error_mark_node)
4123 type = integer_type_node; */
4124 /* That is not done, deliberately, so that having error_mark_node
4125 as the type can suppress useless errors in the use of this variable. */
4127 DECL_NAME (t) = name;
4128 TREE_TYPE (t) = type;
4130 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4131 layout_decl (t, 0);
4133 return t;
4136 /* Builds and returns function declaration with NAME and TYPE. */
4138 tree
4139 build_fn_decl (const char *name, tree type)
4141 tree id = get_identifier (name);
4142 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4144 DECL_EXTERNAL (decl) = 1;
4145 TREE_PUBLIC (decl) = 1;
4146 DECL_ARTIFICIAL (decl) = 1;
4147 TREE_NOTHROW (decl) = 1;
4149 return decl;
4152 vec<tree, va_gc> *all_translation_units;
4154 /* Builds a new translation-unit decl with name NAME, queues it in the
4155 global list of translation-unit decls and returns it. */
4157 tree
4158 build_translation_unit_decl (tree name)
4160 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4161 name, NULL_TREE);
4162 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4163 vec_safe_push (all_translation_units, tu);
4164 return tu;
4168 /* BLOCK nodes are used to represent the structure of binding contours
4169 and declarations, once those contours have been exited and their contents
4170 compiled. This information is used for outputting debugging info. */
4172 tree
4173 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4175 tree block = make_node (BLOCK);
4177 BLOCK_VARS (block) = vars;
4178 BLOCK_SUBBLOCKS (block) = subblocks;
4179 BLOCK_SUPERCONTEXT (block) = supercontext;
4180 BLOCK_CHAIN (block) = chain;
4181 return block;
4185 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4187 LOC is the location to use in tree T. */
4189 void
4190 protected_set_expr_location (tree t, location_t loc)
4192 if (t && CAN_HAVE_LOCATION_P (t))
4193 SET_EXPR_LOCATION (t, loc);
4196 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4197 is ATTRIBUTE. */
4199 tree
4200 build_decl_attribute_variant (tree ddecl, tree attribute)
4202 DECL_ATTRIBUTES (ddecl) = attribute;
4203 return ddecl;
4206 /* Borrowed from hashtab.c iterative_hash implementation. */
4207 #define mix(a,b,c) \
4209 a -= b; a -= c; a ^= (c>>13); \
4210 b -= c; b -= a; b ^= (a<< 8); \
4211 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4212 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4213 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4214 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4215 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4216 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4217 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4221 /* Produce good hash value combining VAL and VAL2. */
4222 hashval_t
4223 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4225 /* the golden ratio; an arbitrary value. */
4226 hashval_t a = 0x9e3779b9;
4228 mix (a, val, val2);
4229 return val2;
4232 /* Produce good hash value combining VAL and VAL2. */
4233 hashval_t
4234 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4236 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4237 return iterative_hash_hashval_t (val, val2);
4238 else
4240 hashval_t a = (hashval_t) val;
4241 /* Avoid warnings about shifting of more than the width of the type on
4242 hosts that won't execute this path. */
4243 int zero = 0;
4244 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4245 mix (a, b, val2);
4246 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4248 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4249 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4250 mix (a, b, val2);
4252 return val2;
4256 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4257 is ATTRIBUTE and its qualifiers are QUALS.
4259 Record such modified types already made so we don't make duplicates. */
4261 tree
4262 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4264 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4266 hashval_t hashcode = 0;
4267 tree ntype;
4268 enum tree_code code = TREE_CODE (ttype);
4270 /* Building a distinct copy of a tagged type is inappropriate; it
4271 causes breakage in code that expects there to be a one-to-one
4272 relationship between a struct and its fields.
4273 build_duplicate_type is another solution (as used in
4274 handle_transparent_union_attribute), but that doesn't play well
4275 with the stronger C++ type identity model. */
4276 if (TREE_CODE (ttype) == RECORD_TYPE
4277 || TREE_CODE (ttype) == UNION_TYPE
4278 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4279 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4281 warning (OPT_Wattributes,
4282 "ignoring attributes applied to %qT after definition",
4283 TYPE_MAIN_VARIANT (ttype));
4284 return build_qualified_type (ttype, quals);
4287 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4288 ntype = build_distinct_type_copy (ttype);
4290 TYPE_ATTRIBUTES (ntype) = attribute;
4292 hashcode = iterative_hash_object (code, hashcode);
4293 if (TREE_TYPE (ntype))
4294 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4295 hashcode);
4296 hashcode = attribute_hash_list (attribute, hashcode);
4298 switch (TREE_CODE (ntype))
4300 case FUNCTION_TYPE:
4301 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4302 break;
4303 case ARRAY_TYPE:
4304 if (TYPE_DOMAIN (ntype))
4305 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4306 hashcode);
4307 break;
4308 case INTEGER_TYPE:
4309 hashcode = iterative_hash_object
4310 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4311 hashcode = iterative_hash_object
4312 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4313 break;
4314 case REAL_TYPE:
4315 case FIXED_POINT_TYPE:
4317 unsigned int precision = TYPE_PRECISION (ntype);
4318 hashcode = iterative_hash_object (precision, hashcode);
4320 break;
4321 default:
4322 break;
4325 ntype = type_hash_canon (hashcode, ntype);
4327 /* If the target-dependent attributes make NTYPE different from
4328 its canonical type, we will need to use structural equality
4329 checks for this type. */
4330 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4331 || !comp_type_attributes (ntype, ttype))
4332 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4333 else if (TYPE_CANONICAL (ntype) == ntype)
4334 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4336 ttype = build_qualified_type (ntype, quals);
4338 else if (TYPE_QUALS (ttype) != quals)
4339 ttype = build_qualified_type (ttype, quals);
4341 return ttype;
4344 /* Compare two attributes for their value identity. Return true if the
4345 attribute values are known to be equal; otherwise return false.
4348 static bool
4349 attribute_value_equal (const_tree attr1, const_tree attr2)
4351 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4352 return true;
4354 if (TREE_VALUE (attr1) != NULL_TREE
4355 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4356 && TREE_VALUE (attr2) != NULL
4357 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4358 return (simple_cst_list_equal (TREE_VALUE (attr1),
4359 TREE_VALUE (attr2)) == 1);
4361 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4364 /* Return 0 if the attributes for two types are incompatible, 1 if they
4365 are compatible, and 2 if they are nearly compatible (which causes a
4366 warning to be generated). */
4368 comp_type_attributes (const_tree type1, const_tree type2)
4370 const_tree a1 = TYPE_ATTRIBUTES (type1);
4371 const_tree a2 = TYPE_ATTRIBUTES (type2);
4372 const_tree a;
4374 if (a1 == a2)
4375 return 1;
4376 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4378 const struct attribute_spec *as;
4379 const_tree attr;
4381 as = lookup_attribute_spec (get_attribute_name (a));
4382 if (!as || as->affects_type_identity == false)
4383 continue;
4385 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4386 if (!attr || !attribute_value_equal (a, attr))
4387 break;
4389 if (!a)
4391 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4393 const struct attribute_spec *as;
4395 as = lookup_attribute_spec (get_attribute_name (a));
4396 if (!as || as->affects_type_identity == false)
4397 continue;
4399 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4400 break;
4401 /* We don't need to compare trees again, as we did this
4402 already in first loop. */
4404 /* All types - affecting identity - are equal, so
4405 there is no need to call target hook for comparison. */
4406 if (!a)
4407 return 1;
4409 /* As some type combinations - like default calling-convention - might
4410 be compatible, we have to call the target hook to get the final result. */
4411 return targetm.comp_type_attributes (type1, type2);
4414 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4415 is ATTRIBUTE.
4417 Record such modified types already made so we don't make duplicates. */
4419 tree
4420 build_type_attribute_variant (tree ttype, tree attribute)
4422 return build_type_attribute_qual_variant (ttype, attribute,
4423 TYPE_QUALS (ttype));
4427 /* Reset the expression *EXPR_P, a size or position.
4429 ??? We could reset all non-constant sizes or positions. But it's cheap
4430 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4432 We need to reset self-referential sizes or positions because they cannot
4433 be gimplified and thus can contain a CALL_EXPR after the gimplification
4434 is finished, which will run afoul of LTO streaming. And they need to be
4435 reset to something essentially dummy but not constant, so as to preserve
4436 the properties of the object they are attached to. */
4438 static inline void
4439 free_lang_data_in_one_sizepos (tree *expr_p)
4441 tree expr = *expr_p;
4442 if (CONTAINS_PLACEHOLDER_P (expr))
4443 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4447 /* Reset all the fields in a binfo node BINFO. We only keep
4448 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4450 static void
4451 free_lang_data_in_binfo (tree binfo)
4453 unsigned i;
4454 tree t;
4456 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4458 BINFO_VIRTUALS (binfo) = NULL_TREE;
4459 BINFO_BASE_ACCESSES (binfo) = NULL;
4460 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4461 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4463 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4464 free_lang_data_in_binfo (t);
4468 /* Reset all language specific information still present in TYPE. */
4470 static void
4471 free_lang_data_in_type (tree type)
4473 gcc_assert (TYPE_P (type));
4475 /* Give the FE a chance to remove its own data first. */
4476 lang_hooks.free_lang_data (type);
4478 TREE_LANG_FLAG_0 (type) = 0;
4479 TREE_LANG_FLAG_1 (type) = 0;
4480 TREE_LANG_FLAG_2 (type) = 0;
4481 TREE_LANG_FLAG_3 (type) = 0;
4482 TREE_LANG_FLAG_4 (type) = 0;
4483 TREE_LANG_FLAG_5 (type) = 0;
4484 TREE_LANG_FLAG_6 (type) = 0;
4486 if (TREE_CODE (type) == FUNCTION_TYPE)
4488 /* Remove the const and volatile qualifiers from arguments. The
4489 C++ front end removes them, but the C front end does not,
4490 leading to false ODR violation errors when merging two
4491 instances of the same function signature compiled by
4492 different front ends. */
4493 tree p;
4495 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4497 tree arg_type = TREE_VALUE (p);
4499 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4501 int quals = TYPE_QUALS (arg_type)
4502 & ~TYPE_QUAL_CONST
4503 & ~TYPE_QUAL_VOLATILE;
4504 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4505 free_lang_data_in_type (TREE_VALUE (p));
4510 /* Remove members that are not actually FIELD_DECLs from the field
4511 list of an aggregate. These occur in C++. */
4512 if (RECORD_OR_UNION_TYPE_P (type))
4514 tree prev, member;
4516 /* Note that TYPE_FIELDS can be shared across distinct
4517 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4518 to be removed, we cannot set its TREE_CHAIN to NULL.
4519 Otherwise, we would not be able to find all the other fields
4520 in the other instances of this TREE_TYPE.
4522 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4523 prev = NULL_TREE;
4524 member = TYPE_FIELDS (type);
4525 while (member)
4527 if (TREE_CODE (member) == FIELD_DECL
4528 || TREE_CODE (member) == TYPE_DECL)
4530 if (prev)
4531 TREE_CHAIN (prev) = member;
4532 else
4533 TYPE_FIELDS (type) = member;
4534 prev = member;
4537 member = TREE_CHAIN (member);
4540 if (prev)
4541 TREE_CHAIN (prev) = NULL_TREE;
4542 else
4543 TYPE_FIELDS (type) = NULL_TREE;
4545 TYPE_METHODS (type) = NULL_TREE;
4546 if (TYPE_BINFO (type))
4547 free_lang_data_in_binfo (TYPE_BINFO (type));
4549 else
4551 /* For non-aggregate types, clear out the language slot (which
4552 overloads TYPE_BINFO). */
4553 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4555 if (INTEGRAL_TYPE_P (type)
4556 || SCALAR_FLOAT_TYPE_P (type)
4557 || FIXED_POINT_TYPE_P (type))
4559 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4560 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4564 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4565 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4567 if (TYPE_CONTEXT (type)
4568 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4570 tree ctx = TYPE_CONTEXT (type);
4573 ctx = BLOCK_SUPERCONTEXT (ctx);
4575 while (ctx && TREE_CODE (ctx) == BLOCK);
4576 TYPE_CONTEXT (type) = ctx;
4581 /* Return true if DECL may need an assembler name to be set. */
4583 static inline bool
4584 need_assembler_name_p (tree decl)
4586 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4587 if (TREE_CODE (decl) != FUNCTION_DECL
4588 && TREE_CODE (decl) != VAR_DECL)
4589 return false;
4591 /* If DECL already has its assembler name set, it does not need a
4592 new one. */
4593 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4594 || DECL_ASSEMBLER_NAME_SET_P (decl))
4595 return false;
4597 /* Abstract decls do not need an assembler name. */
4598 if (DECL_ABSTRACT (decl))
4599 return false;
4601 /* For VAR_DECLs, only static, public and external symbols need an
4602 assembler name. */
4603 if (TREE_CODE (decl) == VAR_DECL
4604 && !TREE_STATIC (decl)
4605 && !TREE_PUBLIC (decl)
4606 && !DECL_EXTERNAL (decl))
4607 return false;
4609 if (TREE_CODE (decl) == FUNCTION_DECL)
4611 /* Do not set assembler name on builtins. Allow RTL expansion to
4612 decide whether to expand inline or via a regular call. */
4613 if (DECL_BUILT_IN (decl)
4614 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4615 return false;
4617 /* Functions represented in the callgraph need an assembler name. */
4618 if (cgraph_get_node (decl) != NULL)
4619 return true;
4621 /* Unused and not public functions don't need an assembler name. */
4622 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4623 return false;
4626 return true;
4630 /* Reset all language specific information still present in symbol
4631 DECL. */
4633 static void
4634 free_lang_data_in_decl (tree decl)
4636 gcc_assert (DECL_P (decl));
4638 /* Give the FE a chance to remove its own data first. */
4639 lang_hooks.free_lang_data (decl);
4641 TREE_LANG_FLAG_0 (decl) = 0;
4642 TREE_LANG_FLAG_1 (decl) = 0;
4643 TREE_LANG_FLAG_2 (decl) = 0;
4644 TREE_LANG_FLAG_3 (decl) = 0;
4645 TREE_LANG_FLAG_4 (decl) = 0;
4646 TREE_LANG_FLAG_5 (decl) = 0;
4647 TREE_LANG_FLAG_6 (decl) = 0;
4649 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4650 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4651 if (TREE_CODE (decl) == FIELD_DECL)
4653 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4654 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4655 DECL_QUALIFIER (decl) = NULL_TREE;
4658 if (TREE_CODE (decl) == FUNCTION_DECL)
4660 if (gimple_has_body_p (decl))
4662 tree t;
4664 /* If DECL has a gimple body, then the context for its
4665 arguments must be DECL. Otherwise, it doesn't really
4666 matter, as we will not be emitting any code for DECL. In
4667 general, there may be other instances of DECL created by
4668 the front end and since PARM_DECLs are generally shared,
4669 their DECL_CONTEXT changes as the replicas of DECL are
4670 created. The only time where DECL_CONTEXT is important
4671 is for the FUNCTION_DECLs that have a gimple body (since
4672 the PARM_DECL will be used in the function's body). */
4673 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4674 DECL_CONTEXT (t) = decl;
4677 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4678 At this point, it is not needed anymore. */
4679 DECL_SAVED_TREE (decl) = NULL_TREE;
4681 /* Clear the abstract origin if it refers to a method. Otherwise
4682 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4683 origin will not be output correctly. */
4684 if (DECL_ABSTRACT_ORIGIN (decl)
4685 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4686 && RECORD_OR_UNION_TYPE_P
4687 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4688 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4690 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4691 DECL_VINDEX referring to itself into a vtable slot number as it
4692 should. Happens with functions that are copied and then forgotten
4693 about. Just clear it, it won't matter anymore. */
4694 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4695 DECL_VINDEX (decl) = NULL_TREE;
4697 else if (TREE_CODE (decl) == VAR_DECL)
4699 if ((DECL_EXTERNAL (decl)
4700 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4701 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4702 DECL_INITIAL (decl) = NULL_TREE;
4704 else if (TREE_CODE (decl) == TYPE_DECL
4705 || TREE_CODE (decl) == FIELD_DECL)
4706 DECL_INITIAL (decl) = NULL_TREE;
4707 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4708 && DECL_INITIAL (decl)
4709 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4711 /* Strip builtins from the translation-unit BLOCK. We still have targets
4712 without builtin_decl_explicit support and also builtins are shared
4713 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4714 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4715 while (*nextp)
4717 tree var = *nextp;
4718 if (TREE_CODE (var) == FUNCTION_DECL
4719 && DECL_BUILT_IN (var))
4720 *nextp = TREE_CHAIN (var);
4721 else
4722 nextp = &TREE_CHAIN (var);
4728 /* Data used when collecting DECLs and TYPEs for language data removal. */
4730 struct free_lang_data_d
4732 /* Worklist to avoid excessive recursion. */
4733 vec<tree> worklist;
4735 /* Set of traversed objects. Used to avoid duplicate visits. */
4736 struct pointer_set_t *pset;
4738 /* Array of symbols to process with free_lang_data_in_decl. */
4739 vec<tree> decls;
4741 /* Array of types to process with free_lang_data_in_type. */
4742 vec<tree> types;
4746 /* Save all language fields needed to generate proper debug information
4747 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4749 static void
4750 save_debug_info_for_decl (tree t)
4752 /*struct saved_debug_info_d *sdi;*/
4754 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4756 /* FIXME. Partial implementation for saving debug info removed. */
4760 /* Save all language fields needed to generate proper debug information
4761 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4763 static void
4764 save_debug_info_for_type (tree t)
4766 /*struct saved_debug_info_d *sdi;*/
4768 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4770 /* FIXME. Partial implementation for saving debug info removed. */
4774 /* Add type or decl T to one of the list of tree nodes that need their
4775 language data removed. The lists are held inside FLD. */
4777 static void
4778 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4780 if (DECL_P (t))
4782 fld->decls.safe_push (t);
4783 if (debug_info_level > DINFO_LEVEL_TERSE)
4784 save_debug_info_for_decl (t);
4786 else if (TYPE_P (t))
4788 fld->types.safe_push (t);
4789 if (debug_info_level > DINFO_LEVEL_TERSE)
4790 save_debug_info_for_type (t);
4792 else
4793 gcc_unreachable ();
4796 /* Push tree node T into FLD->WORKLIST. */
4798 static inline void
4799 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4801 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4802 fld->worklist.safe_push ((t));
4806 /* Operand callback helper for free_lang_data_in_node. *TP is the
4807 subtree operand being considered. */
4809 static tree
4810 find_decls_types_r (tree *tp, int *ws, void *data)
4812 tree t = *tp;
4813 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4815 if (TREE_CODE (t) == TREE_LIST)
4816 return NULL_TREE;
4818 /* Language specific nodes will be removed, so there is no need
4819 to gather anything under them. */
4820 if (is_lang_specific (t))
4822 *ws = 0;
4823 return NULL_TREE;
4826 if (DECL_P (t))
4828 /* Note that walk_tree does not traverse every possible field in
4829 decls, so we have to do our own traversals here. */
4830 add_tree_to_fld_list (t, fld);
4832 fld_worklist_push (DECL_NAME (t), fld);
4833 fld_worklist_push (DECL_CONTEXT (t), fld);
4834 fld_worklist_push (DECL_SIZE (t), fld);
4835 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4837 /* We are going to remove everything under DECL_INITIAL for
4838 TYPE_DECLs. No point walking them. */
4839 if (TREE_CODE (t) != TYPE_DECL)
4840 fld_worklist_push (DECL_INITIAL (t), fld);
4842 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4843 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4845 if (TREE_CODE (t) == FUNCTION_DECL)
4847 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4848 fld_worklist_push (DECL_RESULT (t), fld);
4850 else if (TREE_CODE (t) == TYPE_DECL)
4852 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
4853 fld_worklist_push (DECL_VINDEX (t), fld);
4854 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
4856 else if (TREE_CODE (t) == FIELD_DECL)
4858 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
4859 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
4860 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
4861 fld_worklist_push (DECL_FCONTEXT (t), fld);
4863 else if (TREE_CODE (t) == VAR_DECL)
4865 fld_worklist_push (DECL_SECTION_NAME (t), fld);
4866 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
4869 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
4870 && DECL_HAS_VALUE_EXPR_P (t))
4871 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
4873 if (TREE_CODE (t) != FIELD_DECL
4874 && TREE_CODE (t) != TYPE_DECL)
4875 fld_worklist_push (TREE_CHAIN (t), fld);
4876 *ws = 0;
4878 else if (TYPE_P (t))
4880 /* Note that walk_tree does not traverse every possible field in
4881 types, so we have to do our own traversals here. */
4882 add_tree_to_fld_list (t, fld);
4884 if (!RECORD_OR_UNION_TYPE_P (t))
4885 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
4886 fld_worklist_push (TYPE_SIZE (t), fld);
4887 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
4888 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
4889 fld_worklist_push (TYPE_POINTER_TO (t), fld);
4890 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
4891 fld_worklist_push (TYPE_NAME (t), fld);
4892 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
4893 them and thus do not and want not to reach unused pointer types
4894 this way. */
4895 if (!POINTER_TYPE_P (t))
4896 fld_worklist_push (TYPE_MINVAL (t), fld);
4897 if (!RECORD_OR_UNION_TYPE_P (t))
4898 fld_worklist_push (TYPE_MAXVAL (t), fld);
4899 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
4900 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
4901 do not and want not to reach unused variants this way. */
4902 if (TYPE_CONTEXT (t))
4904 tree ctx = TYPE_CONTEXT (t);
4905 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
4906 So push that instead. */
4907 while (ctx && TREE_CODE (ctx) == BLOCK)
4908 ctx = BLOCK_SUPERCONTEXT (ctx);
4909 fld_worklist_push (ctx, fld);
4911 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
4912 and want not to reach unused types this way. */
4914 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
4916 unsigned i;
4917 tree tem;
4918 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
4919 fld_worklist_push (TREE_TYPE (tem), fld);
4920 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
4921 if (tem
4922 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
4923 && TREE_CODE (tem) == TREE_LIST)
4926 fld_worklist_push (TREE_VALUE (tem), fld);
4927 tem = TREE_CHAIN (tem);
4929 while (tem);
4931 if (RECORD_OR_UNION_TYPE_P (t))
4933 tree tem;
4934 /* Push all TYPE_FIELDS - there can be interleaving interesting
4935 and non-interesting things. */
4936 tem = TYPE_FIELDS (t);
4937 while (tem)
4939 if (TREE_CODE (tem) == FIELD_DECL
4940 || TREE_CODE (tem) == TYPE_DECL)
4941 fld_worklist_push (tem, fld);
4942 tem = TREE_CHAIN (tem);
4946 fld_worklist_push (TYPE_STUB_DECL (t), fld);
4947 *ws = 0;
4949 else if (TREE_CODE (t) == BLOCK)
4951 tree tem;
4952 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
4953 fld_worklist_push (tem, fld);
4954 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
4955 fld_worklist_push (tem, fld);
4956 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
4959 if (TREE_CODE (t) != IDENTIFIER_NODE
4960 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
4961 fld_worklist_push (TREE_TYPE (t), fld);
4963 return NULL_TREE;
4967 /* Find decls and types in T. */
4969 static void
4970 find_decls_types (tree t, struct free_lang_data_d *fld)
4972 while (1)
4974 if (!pointer_set_contains (fld->pset, t))
4975 walk_tree (&t, find_decls_types_r, fld, fld->pset);
4976 if (fld->worklist.is_empty ())
4977 break;
4978 t = fld->worklist.pop ();
4982 /* Translate all the types in LIST with the corresponding runtime
4983 types. */
4985 static tree
4986 get_eh_types_for_runtime (tree list)
4988 tree head, prev;
4990 if (list == NULL_TREE)
4991 return NULL_TREE;
4993 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
4994 prev = head;
4995 list = TREE_CHAIN (list);
4996 while (list)
4998 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
4999 TREE_CHAIN (prev) = n;
5000 prev = TREE_CHAIN (prev);
5001 list = TREE_CHAIN (list);
5004 return head;
5008 /* Find decls and types referenced in EH region R and store them in
5009 FLD->DECLS and FLD->TYPES. */
5011 static void
5012 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5014 switch (r->type)
5016 case ERT_CLEANUP:
5017 break;
5019 case ERT_TRY:
5021 eh_catch c;
5023 /* The types referenced in each catch must first be changed to the
5024 EH types used at runtime. This removes references to FE types
5025 in the region. */
5026 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5028 c->type_list = get_eh_types_for_runtime (c->type_list);
5029 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5032 break;
5034 case ERT_ALLOWED_EXCEPTIONS:
5035 r->u.allowed.type_list
5036 = get_eh_types_for_runtime (r->u.allowed.type_list);
5037 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5038 break;
5040 case ERT_MUST_NOT_THROW:
5041 walk_tree (&r->u.must_not_throw.failure_decl,
5042 find_decls_types_r, fld, fld->pset);
5043 break;
5048 /* Find decls and types referenced in cgraph node N and store them in
5049 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5050 look for *every* kind of DECL and TYPE node reachable from N,
5051 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5052 NAMESPACE_DECLs, etc). */
5054 static void
5055 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5057 basic_block bb;
5058 struct function *fn;
5059 unsigned ix;
5060 tree t;
5062 find_decls_types (n->symbol.decl, fld);
5064 if (!gimple_has_body_p (n->symbol.decl))
5065 return;
5067 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5069 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5071 /* Traverse locals. */
5072 FOR_EACH_LOCAL_DECL (fn, ix, t)
5073 find_decls_types (t, fld);
5075 /* Traverse EH regions in FN. */
5077 eh_region r;
5078 FOR_ALL_EH_REGION_FN (r, fn)
5079 find_decls_types_in_eh_region (r, fld);
5082 /* Traverse every statement in FN. */
5083 FOR_EACH_BB_FN (bb, fn)
5085 gimple_stmt_iterator si;
5086 unsigned i;
5088 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5090 gimple phi = gsi_stmt (si);
5092 for (i = 0; i < gimple_phi_num_args (phi); i++)
5094 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5095 find_decls_types (*arg_p, fld);
5099 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5101 gimple stmt = gsi_stmt (si);
5103 if (is_gimple_call (stmt))
5104 find_decls_types (gimple_call_fntype (stmt), fld);
5106 for (i = 0; i < gimple_num_ops (stmt); i++)
5108 tree arg = gimple_op (stmt, i);
5109 find_decls_types (arg, fld);
5116 /* Find decls and types referenced in varpool node N and store them in
5117 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5118 look for *every* kind of DECL and TYPE node reachable from N,
5119 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5120 NAMESPACE_DECLs, etc). */
5122 static void
5123 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5125 find_decls_types (v->symbol.decl, fld);
5128 /* If T needs an assembler name, have one created for it. */
5130 void
5131 assign_assembler_name_if_neeeded (tree t)
5133 if (need_assembler_name_p (t))
5135 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5136 diagnostics that use input_location to show locus
5137 information. The problem here is that, at this point,
5138 input_location is generally anchored to the end of the file
5139 (since the parser is long gone), so we don't have a good
5140 position to pin it to.
5142 To alleviate this problem, this uses the location of T's
5143 declaration. Examples of this are
5144 testsuite/g++.dg/template/cond2.C and
5145 testsuite/g++.dg/template/pr35240.C. */
5146 location_t saved_location = input_location;
5147 input_location = DECL_SOURCE_LOCATION (t);
5149 decl_assembler_name (t);
5151 input_location = saved_location;
5156 /* Free language specific information for every operand and expression
5157 in every node of the call graph. This process operates in three stages:
5159 1- Every callgraph node and varpool node is traversed looking for
5160 decls and types embedded in them. This is a more exhaustive
5161 search than that done by find_referenced_vars, because it will
5162 also collect individual fields, decls embedded in types, etc.
5164 2- All the decls found are sent to free_lang_data_in_decl.
5166 3- All the types found are sent to free_lang_data_in_type.
5168 The ordering between decls and types is important because
5169 free_lang_data_in_decl sets assembler names, which includes
5170 mangling. So types cannot be freed up until assembler names have
5171 been set up. */
5173 static void
5174 free_lang_data_in_cgraph (void)
5176 struct cgraph_node *n;
5177 struct varpool_node *v;
5178 struct free_lang_data_d fld;
5179 tree t;
5180 unsigned i;
5181 alias_pair *p;
5183 /* Initialize sets and arrays to store referenced decls and types. */
5184 fld.pset = pointer_set_create ();
5185 fld.worklist.create (0);
5186 fld.decls.create (100);
5187 fld.types.create (100);
5189 /* Find decls and types in the body of every function in the callgraph. */
5190 FOR_EACH_FUNCTION (n)
5191 find_decls_types_in_node (n, &fld);
5193 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5194 find_decls_types (p->decl, &fld);
5196 /* Find decls and types in every varpool symbol. */
5197 FOR_EACH_VARIABLE (v)
5198 find_decls_types_in_var (v, &fld);
5200 /* Set the assembler name on every decl found. We need to do this
5201 now because free_lang_data_in_decl will invalidate data needed
5202 for mangling. This breaks mangling on interdependent decls. */
5203 FOR_EACH_VEC_ELT (fld.decls, i, t)
5204 assign_assembler_name_if_neeeded (t);
5206 /* Traverse every decl found freeing its language data. */
5207 FOR_EACH_VEC_ELT (fld.decls, i, t)
5208 free_lang_data_in_decl (t);
5210 /* Traverse every type found freeing its language data. */
5211 FOR_EACH_VEC_ELT (fld.types, i, t)
5212 free_lang_data_in_type (t);
5214 pointer_set_destroy (fld.pset);
5215 fld.worklist.release ();
5216 fld.decls.release ();
5217 fld.types.release ();
5221 /* Free resources that are used by FE but are not needed once they are done. */
5223 static unsigned
5224 free_lang_data (void)
5226 unsigned i;
5228 /* If we are the LTO frontend we have freed lang-specific data already. */
5229 if (in_lto_p
5230 || !flag_generate_lto)
5231 return 0;
5233 /* Allocate and assign alias sets to the standard integer types
5234 while the slots are still in the way the frontends generated them. */
5235 for (i = 0; i < itk_none; ++i)
5236 if (integer_types[i])
5237 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5239 /* Traverse the IL resetting language specific information for
5240 operands, expressions, etc. */
5241 free_lang_data_in_cgraph ();
5243 /* Create gimple variants for common types. */
5244 ptrdiff_type_node = integer_type_node;
5245 fileptr_type_node = ptr_type_node;
5247 /* Reset some langhooks. Do not reset types_compatible_p, it may
5248 still be used indirectly via the get_alias_set langhook. */
5249 lang_hooks.dwarf_name = lhd_dwarf_name;
5250 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5251 /* We do not want the default decl_assembler_name implementation,
5252 rather if we have fixed everything we want a wrapper around it
5253 asserting that all non-local symbols already got their assembler
5254 name and only produce assembler names for local symbols. Or rather
5255 make sure we never call decl_assembler_name on local symbols and
5256 devise a separate, middle-end private scheme for it. */
5258 /* Reset diagnostic machinery. */
5259 tree_diagnostics_defaults (global_dc);
5261 return 0;
5265 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5268 SIMPLE_IPA_PASS,
5269 "*free_lang_data", /* name */
5270 OPTGROUP_NONE, /* optinfo_flags */
5271 NULL, /* gate */
5272 free_lang_data, /* execute */
5273 NULL, /* sub */
5274 NULL, /* next */
5275 0, /* static_pass_number */
5276 TV_IPA_FREE_LANG_DATA, /* tv_id */
5277 0, /* properties_required */
5278 0, /* properties_provided */
5279 0, /* properties_destroyed */
5280 0, /* todo_flags_start */
5281 TODO_ggc_collect /* todo_flags_finish */
5285 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5286 ATTR_NAME. Also used internally by remove_attribute(). */
5287 bool
5288 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5290 size_t ident_len = IDENTIFIER_LENGTH (ident);
5292 if (ident_len == attr_len)
5294 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5295 return true;
5297 else if (ident_len == attr_len + 4)
5299 /* There is the possibility that ATTR is 'text' and IDENT is
5300 '__text__'. */
5301 const char *p = IDENTIFIER_POINTER (ident);
5302 if (p[0] == '_' && p[1] == '_'
5303 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5304 && strncmp (attr_name, p + 2, attr_len) == 0)
5305 return true;
5308 return false;
5311 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5312 of ATTR_NAME, and LIST is not NULL_TREE. */
5313 tree
5314 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5316 while (list)
5318 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5320 if (ident_len == attr_len)
5322 if (!strcmp (attr_name,
5323 IDENTIFIER_POINTER (get_attribute_name (list))))
5324 break;
5326 /* TODO: If we made sure that attributes were stored in the
5327 canonical form without '__...__' (ie, as in 'text' as opposed
5328 to '__text__') then we could avoid the following case. */
5329 else if (ident_len == attr_len + 4)
5331 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5332 if (p[0] == '_' && p[1] == '_'
5333 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5334 && strncmp (attr_name, p + 2, attr_len) == 0)
5335 break;
5337 list = TREE_CHAIN (list);
5340 return list;
5343 /* A variant of lookup_attribute() that can be used with an identifier
5344 as the first argument, and where the identifier can be either
5345 'text' or '__text__'.
5347 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5348 return a pointer to the attribute's list element if the attribute
5349 is part of the list, or NULL_TREE if not found. If the attribute
5350 appears more than once, this only returns the first occurrence; the
5351 TREE_CHAIN of the return value should be passed back in if further
5352 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5353 can be in the form 'text' or '__text__'. */
5354 static tree
5355 lookup_ident_attribute (tree attr_identifier, tree list)
5357 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5359 while (list)
5361 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5362 == IDENTIFIER_NODE);
5364 /* Identifiers can be compared directly for equality. */
5365 if (attr_identifier == get_attribute_name (list))
5366 break;
5368 /* If they are not equal, they may still be one in the form
5369 'text' while the other one is in the form '__text__'. TODO:
5370 If we were storing attributes in normalized 'text' form, then
5371 this could all go away and we could take full advantage of
5372 the fact that we're comparing identifiers. :-) */
5374 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5375 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5377 if (ident_len == attr_len + 4)
5379 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5380 const char *q = IDENTIFIER_POINTER (attr_identifier);
5381 if (p[0] == '_' && p[1] == '_'
5382 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5383 && strncmp (q, p + 2, attr_len) == 0)
5384 break;
5386 else if (ident_len + 4 == attr_len)
5388 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5389 const char *q = IDENTIFIER_POINTER (attr_identifier);
5390 if (q[0] == '_' && q[1] == '_'
5391 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5392 && strncmp (q + 2, p, ident_len) == 0)
5393 break;
5396 list = TREE_CHAIN (list);
5399 return list;
5402 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5403 modified list. */
5405 tree
5406 remove_attribute (const char *attr_name, tree list)
5408 tree *p;
5409 size_t attr_len = strlen (attr_name);
5411 gcc_checking_assert (attr_name[0] != '_');
5413 for (p = &list; *p; )
5415 tree l = *p;
5416 /* TODO: If we were storing attributes in normalized form, here
5417 we could use a simple strcmp(). */
5418 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5419 *p = TREE_CHAIN (l);
5420 else
5421 p = &TREE_CHAIN (l);
5424 return list;
5427 /* Return an attribute list that is the union of a1 and a2. */
5429 tree
5430 merge_attributes (tree a1, tree a2)
5432 tree attributes;
5434 /* Either one unset? Take the set one. */
5436 if ((attributes = a1) == 0)
5437 attributes = a2;
5439 /* One that completely contains the other? Take it. */
5441 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5443 if (attribute_list_contained (a2, a1))
5444 attributes = a2;
5445 else
5447 /* Pick the longest list, and hang on the other list. */
5449 if (list_length (a1) < list_length (a2))
5450 attributes = a2, a2 = a1;
5452 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5454 tree a;
5455 for (a = lookup_ident_attribute (get_attribute_name (a2),
5456 attributes);
5457 a != NULL_TREE && !attribute_value_equal (a, a2);
5458 a = lookup_ident_attribute (get_attribute_name (a2),
5459 TREE_CHAIN (a)))
5461 if (a == NULL_TREE)
5463 a1 = copy_node (a2);
5464 TREE_CHAIN (a1) = attributes;
5465 attributes = a1;
5470 return attributes;
5473 /* Given types T1 and T2, merge their attributes and return
5474 the result. */
5476 tree
5477 merge_type_attributes (tree t1, tree t2)
5479 return merge_attributes (TYPE_ATTRIBUTES (t1),
5480 TYPE_ATTRIBUTES (t2));
5483 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5484 the result. */
5486 tree
5487 merge_decl_attributes (tree olddecl, tree newdecl)
5489 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5490 DECL_ATTRIBUTES (newdecl));
5493 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5495 /* Specialization of merge_decl_attributes for various Windows targets.
5497 This handles the following situation:
5499 __declspec (dllimport) int foo;
5500 int foo;
5502 The second instance of `foo' nullifies the dllimport. */
5504 tree
5505 merge_dllimport_decl_attributes (tree old, tree new_tree)
5507 tree a;
5508 int delete_dllimport_p = 1;
5510 /* What we need to do here is remove from `old' dllimport if it doesn't
5511 appear in `new'. dllimport behaves like extern: if a declaration is
5512 marked dllimport and a definition appears later, then the object
5513 is not dllimport'd. We also remove a `new' dllimport if the old list
5514 contains dllexport: dllexport always overrides dllimport, regardless
5515 of the order of declaration. */
5516 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5517 delete_dllimport_p = 0;
5518 else if (DECL_DLLIMPORT_P (new_tree)
5519 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5521 DECL_DLLIMPORT_P (new_tree) = 0;
5522 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5523 "dllimport ignored", new_tree);
5525 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5527 /* Warn about overriding a symbol that has already been used, e.g.:
5528 extern int __attribute__ ((dllimport)) foo;
5529 int* bar () {return &foo;}
5530 int foo;
5532 if (TREE_USED (old))
5534 warning (0, "%q+D redeclared without dllimport attribute "
5535 "after being referenced with dll linkage", new_tree);
5536 /* If we have used a variable's address with dllimport linkage,
5537 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5538 decl may already have had TREE_CONSTANT computed.
5539 We still remove the attribute so that assembler code refers
5540 to '&foo rather than '_imp__foo'. */
5541 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5542 DECL_DLLIMPORT_P (new_tree) = 1;
5545 /* Let an inline definition silently override the external reference,
5546 but otherwise warn about attribute inconsistency. */
5547 else if (TREE_CODE (new_tree) == VAR_DECL
5548 || !DECL_DECLARED_INLINE_P (new_tree))
5549 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5550 "previous dllimport ignored", new_tree);
5552 else
5553 delete_dllimport_p = 0;
5555 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5557 if (delete_dllimport_p)
5558 a = remove_attribute ("dllimport", a);
5560 return a;
5563 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5564 struct attribute_spec.handler. */
5566 tree
5567 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5568 bool *no_add_attrs)
5570 tree node = *pnode;
5571 bool is_dllimport;
5573 /* These attributes may apply to structure and union types being created,
5574 but otherwise should pass to the declaration involved. */
5575 if (!DECL_P (node))
5577 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5578 | (int) ATTR_FLAG_ARRAY_NEXT))
5580 *no_add_attrs = true;
5581 return tree_cons (name, args, NULL_TREE);
5583 if (TREE_CODE (node) == RECORD_TYPE
5584 || TREE_CODE (node) == UNION_TYPE)
5586 node = TYPE_NAME (node);
5587 if (!node)
5588 return NULL_TREE;
5590 else
5592 warning (OPT_Wattributes, "%qE attribute ignored",
5593 name);
5594 *no_add_attrs = true;
5595 return NULL_TREE;
5599 if (TREE_CODE (node) != FUNCTION_DECL
5600 && TREE_CODE (node) != VAR_DECL
5601 && TREE_CODE (node) != TYPE_DECL)
5603 *no_add_attrs = true;
5604 warning (OPT_Wattributes, "%qE attribute ignored",
5605 name);
5606 return NULL_TREE;
5609 if (TREE_CODE (node) == TYPE_DECL
5610 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5611 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5613 *no_add_attrs = true;
5614 warning (OPT_Wattributes, "%qE attribute ignored",
5615 name);
5616 return NULL_TREE;
5619 is_dllimport = is_attribute_p ("dllimport", name);
5621 /* Report error on dllimport ambiguities seen now before they cause
5622 any damage. */
5623 if (is_dllimport)
5625 /* Honor any target-specific overrides. */
5626 if (!targetm.valid_dllimport_attribute_p (node))
5627 *no_add_attrs = true;
5629 else if (TREE_CODE (node) == FUNCTION_DECL
5630 && DECL_DECLARED_INLINE_P (node))
5632 warning (OPT_Wattributes, "inline function %q+D declared as "
5633 " dllimport: attribute ignored", node);
5634 *no_add_attrs = true;
5636 /* Like MS, treat definition of dllimported variables and
5637 non-inlined functions on declaration as syntax errors. */
5638 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5640 error ("function %q+D definition is marked dllimport", node);
5641 *no_add_attrs = true;
5644 else if (TREE_CODE (node) == VAR_DECL)
5646 if (DECL_INITIAL (node))
5648 error ("variable %q+D definition is marked dllimport",
5649 node);
5650 *no_add_attrs = true;
5653 /* `extern' needn't be specified with dllimport.
5654 Specify `extern' now and hope for the best. Sigh. */
5655 DECL_EXTERNAL (node) = 1;
5656 /* Also, implicitly give dllimport'd variables declared within
5657 a function global scope, unless declared static. */
5658 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5659 TREE_PUBLIC (node) = 1;
5662 if (*no_add_attrs == false)
5663 DECL_DLLIMPORT_P (node) = 1;
5665 else if (TREE_CODE (node) == FUNCTION_DECL
5666 && DECL_DECLARED_INLINE_P (node)
5667 && flag_keep_inline_dllexport)
5668 /* An exported function, even if inline, must be emitted. */
5669 DECL_EXTERNAL (node) = 0;
5671 /* Report error if symbol is not accessible at global scope. */
5672 if (!TREE_PUBLIC (node)
5673 && (TREE_CODE (node) == VAR_DECL
5674 || TREE_CODE (node) == FUNCTION_DECL))
5676 error ("external linkage required for symbol %q+D because of "
5677 "%qE attribute", node, name);
5678 *no_add_attrs = true;
5681 /* A dllexport'd entity must have default visibility so that other
5682 program units (shared libraries or the main executable) can see
5683 it. A dllimport'd entity must have default visibility so that
5684 the linker knows that undefined references within this program
5685 unit can be resolved by the dynamic linker. */
5686 if (!*no_add_attrs)
5688 if (DECL_VISIBILITY_SPECIFIED (node)
5689 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5690 error ("%qE implies default visibility, but %qD has already "
5691 "been declared with a different visibility",
5692 name, node);
5693 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5694 DECL_VISIBILITY_SPECIFIED (node) = 1;
5697 return NULL_TREE;
5700 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5702 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5703 of the various TYPE_QUAL values. */
5705 static void
5706 set_type_quals (tree type, int type_quals)
5708 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5709 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5710 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5711 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5714 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
5716 bool
5717 check_qualified_type (const_tree cand, const_tree base, int type_quals)
5719 return (TYPE_QUALS (cand) == type_quals
5720 && TYPE_NAME (cand) == TYPE_NAME (base)
5721 /* Apparently this is needed for Objective-C. */
5722 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5723 /* Check alignment. */
5724 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5725 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5726 TYPE_ATTRIBUTES (base)));
5729 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5731 static bool
5732 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5734 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5735 && TYPE_NAME (cand) == TYPE_NAME (base)
5736 /* Apparently this is needed for Objective-C. */
5737 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5738 /* Check alignment. */
5739 && TYPE_ALIGN (cand) == align
5740 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5741 TYPE_ATTRIBUTES (base)));
5744 /* Return a version of the TYPE, qualified as indicated by the
5745 TYPE_QUALS, if one exists. If no qualified version exists yet,
5746 return NULL_TREE. */
5748 tree
5749 get_qualified_type (tree type, int type_quals)
5751 tree t;
5753 if (TYPE_QUALS (type) == type_quals)
5754 return type;
5756 /* Search the chain of variants to see if there is already one there just
5757 like the one we need to have. If so, use that existing one. We must
5758 preserve the TYPE_NAME, since there is code that depends on this. */
5759 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5760 if (check_qualified_type (t, type, type_quals))
5761 return t;
5763 return NULL_TREE;
5766 /* Like get_qualified_type, but creates the type if it does not
5767 exist. This function never returns NULL_TREE. */
5769 tree
5770 build_qualified_type (tree type, int type_quals)
5772 tree t;
5774 /* See if we already have the appropriate qualified variant. */
5775 t = get_qualified_type (type, type_quals);
5777 /* If not, build it. */
5778 if (!t)
5780 t = build_variant_type_copy (type);
5781 set_type_quals (t, type_quals);
5783 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5784 /* Propagate structural equality. */
5785 SET_TYPE_STRUCTURAL_EQUALITY (t);
5786 else if (TYPE_CANONICAL (type) != type)
5787 /* Build the underlying canonical type, since it is different
5788 from TYPE. */
5789 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5790 type_quals);
5791 else
5792 /* T is its own canonical type. */
5793 TYPE_CANONICAL (t) = t;
5797 return t;
5800 /* Create a variant of type T with alignment ALIGN. */
5802 tree
5803 build_aligned_type (tree type, unsigned int align)
5805 tree t;
5807 if (TYPE_PACKED (type)
5808 || TYPE_ALIGN (type) == align)
5809 return type;
5811 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5812 if (check_aligned_type (t, type, align))
5813 return t;
5815 t = build_variant_type_copy (type);
5816 TYPE_ALIGN (t) = align;
5818 return t;
5821 /* Create a new distinct copy of TYPE. The new type is made its own
5822 MAIN_VARIANT. If TYPE requires structural equality checks, the
5823 resulting type requires structural equality checks; otherwise, its
5824 TYPE_CANONICAL points to itself. */
5826 tree
5827 build_distinct_type_copy (tree type)
5829 tree t = copy_node (type);
5831 TYPE_POINTER_TO (t) = 0;
5832 TYPE_REFERENCE_TO (t) = 0;
5834 /* Set the canonical type either to a new equivalence class, or
5835 propagate the need for structural equality checks. */
5836 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5837 SET_TYPE_STRUCTURAL_EQUALITY (t);
5838 else
5839 TYPE_CANONICAL (t) = t;
5841 /* Make it its own variant. */
5842 TYPE_MAIN_VARIANT (t) = t;
5843 TYPE_NEXT_VARIANT (t) = 0;
5845 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
5846 whose TREE_TYPE is not t. This can also happen in the Ada
5847 frontend when using subtypes. */
5849 return t;
5852 /* Create a new variant of TYPE, equivalent but distinct. This is so
5853 the caller can modify it. TYPE_CANONICAL for the return type will
5854 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
5855 are considered equal by the language itself (or that both types
5856 require structural equality checks). */
5858 tree
5859 build_variant_type_copy (tree type)
5861 tree t, m = TYPE_MAIN_VARIANT (type);
5863 t = build_distinct_type_copy (type);
5865 /* Since we're building a variant, assume that it is a non-semantic
5866 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
5867 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
5869 /* Add the new type to the chain of variants of TYPE. */
5870 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
5871 TYPE_NEXT_VARIANT (m) = t;
5872 TYPE_MAIN_VARIANT (t) = m;
5874 return t;
5877 /* Return true if the from tree in both tree maps are equal. */
5880 tree_map_base_eq (const void *va, const void *vb)
5882 const struct tree_map_base *const a = (const struct tree_map_base *) va,
5883 *const b = (const struct tree_map_base *) vb;
5884 return (a->from == b->from);
5887 /* Hash a from tree in a tree_base_map. */
5889 unsigned int
5890 tree_map_base_hash (const void *item)
5892 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
5895 /* Return true if this tree map structure is marked for garbage collection
5896 purposes. We simply return true if the from tree is marked, so that this
5897 structure goes away when the from tree goes away. */
5900 tree_map_base_marked_p (const void *p)
5902 return ggc_marked_p (((const struct tree_map_base *) p)->from);
5905 /* Hash a from tree in a tree_map. */
5907 unsigned int
5908 tree_map_hash (const void *item)
5910 return (((const struct tree_map *) item)->hash);
5913 /* Hash a from tree in a tree_decl_map. */
5915 unsigned int
5916 tree_decl_map_hash (const void *item)
5918 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
5921 /* Return the initialization priority for DECL. */
5923 priority_type
5924 decl_init_priority_lookup (tree decl)
5926 struct tree_priority_map *h;
5927 struct tree_map_base in;
5929 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5930 in.from = decl;
5931 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5932 return h ? h->init : DEFAULT_INIT_PRIORITY;
5935 /* Return the finalization priority for DECL. */
5937 priority_type
5938 decl_fini_priority_lookup (tree decl)
5940 struct tree_priority_map *h;
5941 struct tree_map_base in;
5943 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5944 in.from = decl;
5945 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
5946 return h ? h->fini : DEFAULT_INIT_PRIORITY;
5949 /* Return the initialization and finalization priority information for
5950 DECL. If there is no previous priority information, a freshly
5951 allocated structure is returned. */
5953 static struct tree_priority_map *
5954 decl_priority_info (tree decl)
5956 struct tree_priority_map in;
5957 struct tree_priority_map *h;
5958 void **loc;
5960 in.base.from = decl;
5961 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
5962 h = (struct tree_priority_map *) *loc;
5963 if (!h)
5965 h = ggc_alloc_cleared_tree_priority_map ();
5966 *loc = h;
5967 h->base.from = decl;
5968 h->init = DEFAULT_INIT_PRIORITY;
5969 h->fini = DEFAULT_INIT_PRIORITY;
5972 return h;
5975 /* Set the initialization priority for DECL to PRIORITY. */
5977 void
5978 decl_init_priority_insert (tree decl, priority_type priority)
5980 struct tree_priority_map *h;
5982 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5983 if (priority == DEFAULT_INIT_PRIORITY)
5984 return;
5985 h = decl_priority_info (decl);
5986 h->init = priority;
5989 /* Set the finalization priority for DECL to PRIORITY. */
5991 void
5992 decl_fini_priority_insert (tree decl, priority_type priority)
5994 struct tree_priority_map *h;
5996 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5997 if (priority == DEFAULT_INIT_PRIORITY)
5998 return;
5999 h = decl_priority_info (decl);
6000 h->fini = priority;
6003 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6005 static void
6006 print_debug_expr_statistics (void)
6008 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6009 (long) htab_size (debug_expr_for_decl),
6010 (long) htab_elements (debug_expr_for_decl),
6011 htab_collisions (debug_expr_for_decl));
6014 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6016 static void
6017 print_value_expr_statistics (void)
6019 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6020 (long) htab_size (value_expr_for_decl),
6021 (long) htab_elements (value_expr_for_decl),
6022 htab_collisions (value_expr_for_decl));
6025 /* Lookup a debug expression for FROM, and return it if we find one. */
6027 tree
6028 decl_debug_expr_lookup (tree from)
6030 struct tree_decl_map *h, in;
6031 in.base.from = from;
6033 h = (struct tree_decl_map *)
6034 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6035 if (h)
6036 return h->to;
6037 return NULL_TREE;
6040 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6042 void
6043 decl_debug_expr_insert (tree from, tree to)
6045 struct tree_decl_map *h;
6046 void **loc;
6048 h = ggc_alloc_tree_decl_map ();
6049 h->base.from = from;
6050 h->to = to;
6051 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6052 INSERT);
6053 *(struct tree_decl_map **) loc = h;
6056 /* Lookup a value expression for FROM, and return it if we find one. */
6058 tree
6059 decl_value_expr_lookup (tree from)
6061 struct tree_decl_map *h, in;
6062 in.base.from = from;
6064 h = (struct tree_decl_map *)
6065 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6066 if (h)
6067 return h->to;
6068 return NULL_TREE;
6071 /* Insert a mapping FROM->TO in the value expression hashtable. */
6073 void
6074 decl_value_expr_insert (tree from, tree to)
6076 struct tree_decl_map *h;
6077 void **loc;
6079 h = ggc_alloc_tree_decl_map ();
6080 h->base.from = from;
6081 h->to = to;
6082 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6083 INSERT);
6084 *(struct tree_decl_map **) loc = h;
6087 /* Lookup a vector of debug arguments for FROM, and return it if we
6088 find one. */
6090 vec<tree, va_gc> **
6091 decl_debug_args_lookup (tree from)
6093 struct tree_vec_map *h, in;
6095 if (!DECL_HAS_DEBUG_ARGS_P (from))
6096 return NULL;
6097 gcc_checking_assert (debug_args_for_decl != NULL);
6098 in.base.from = from;
6099 h = (struct tree_vec_map *)
6100 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6101 if (h)
6102 return &h->to;
6103 return NULL;
6106 /* Insert a mapping FROM->empty vector of debug arguments in the value
6107 expression hashtable. */
6109 vec<tree, va_gc> **
6110 decl_debug_args_insert (tree from)
6112 struct tree_vec_map *h;
6113 void **loc;
6115 if (DECL_HAS_DEBUG_ARGS_P (from))
6116 return decl_debug_args_lookup (from);
6117 if (debug_args_for_decl == NULL)
6118 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6119 tree_vec_map_eq, 0);
6120 h = ggc_alloc_tree_vec_map ();
6121 h->base.from = from;
6122 h->to = NULL;
6123 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6124 INSERT);
6125 *(struct tree_vec_map **) loc = h;
6126 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6127 return &h->to;
6130 /* Hashing of types so that we don't make duplicates.
6131 The entry point is `type_hash_canon'. */
6133 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6134 with types in the TREE_VALUE slots), by adding the hash codes
6135 of the individual types. */
6137 static unsigned int
6138 type_hash_list (const_tree list, hashval_t hashcode)
6140 const_tree tail;
6142 for (tail = list; tail; tail = TREE_CHAIN (tail))
6143 if (TREE_VALUE (tail) != error_mark_node)
6144 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6145 hashcode);
6147 return hashcode;
6150 /* These are the Hashtable callback functions. */
6152 /* Returns true iff the types are equivalent. */
6154 static int
6155 type_hash_eq (const void *va, const void *vb)
6157 const struct type_hash *const a = (const struct type_hash *) va,
6158 *const b = (const struct type_hash *) vb;
6160 /* First test the things that are the same for all types. */
6161 if (a->hash != b->hash
6162 || TREE_CODE (a->type) != TREE_CODE (b->type)
6163 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6164 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6165 TYPE_ATTRIBUTES (b->type))
6166 || (TREE_CODE (a->type) != COMPLEX_TYPE
6167 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6168 return 0;
6170 /* Be careful about comparing arrays before and after the element type
6171 has been completed; don't compare TYPE_ALIGN unless both types are
6172 complete. */
6173 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6174 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6175 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6176 return 0;
6178 switch (TREE_CODE (a->type))
6180 case VOID_TYPE:
6181 case COMPLEX_TYPE:
6182 case POINTER_TYPE:
6183 case REFERENCE_TYPE:
6184 case NULLPTR_TYPE:
6185 return 1;
6187 case VECTOR_TYPE:
6188 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6190 case ENUMERAL_TYPE:
6191 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6192 && !(TYPE_VALUES (a->type)
6193 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6194 && TYPE_VALUES (b->type)
6195 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6196 && type_list_equal (TYPE_VALUES (a->type),
6197 TYPE_VALUES (b->type))))
6198 return 0;
6200 /* ... fall through ... */
6202 case INTEGER_TYPE:
6203 case REAL_TYPE:
6204 case BOOLEAN_TYPE:
6205 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6206 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6207 TYPE_MAX_VALUE (b->type)))
6208 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6209 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6210 TYPE_MIN_VALUE (b->type))));
6212 case FIXED_POINT_TYPE:
6213 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6215 case OFFSET_TYPE:
6216 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6218 case METHOD_TYPE:
6219 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6220 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6221 || (TYPE_ARG_TYPES (a->type)
6222 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6223 && TYPE_ARG_TYPES (b->type)
6224 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6225 && type_list_equal (TYPE_ARG_TYPES (a->type),
6226 TYPE_ARG_TYPES (b->type)))))
6227 break;
6228 return 0;
6229 case ARRAY_TYPE:
6230 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6232 case RECORD_TYPE:
6233 case UNION_TYPE:
6234 case QUAL_UNION_TYPE:
6235 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6236 || (TYPE_FIELDS (a->type)
6237 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6238 && TYPE_FIELDS (b->type)
6239 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6240 && type_list_equal (TYPE_FIELDS (a->type),
6241 TYPE_FIELDS (b->type))));
6243 case FUNCTION_TYPE:
6244 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6245 || (TYPE_ARG_TYPES (a->type)
6246 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6247 && TYPE_ARG_TYPES (b->type)
6248 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6249 && type_list_equal (TYPE_ARG_TYPES (a->type),
6250 TYPE_ARG_TYPES (b->type))))
6251 break;
6252 return 0;
6254 default:
6255 return 0;
6258 if (lang_hooks.types.type_hash_eq != NULL)
6259 return lang_hooks.types.type_hash_eq (a->type, b->type);
6261 return 1;
6264 /* Return the cached hash value. */
6266 static hashval_t
6267 type_hash_hash (const void *item)
6269 return ((const struct type_hash *) item)->hash;
6272 /* Look in the type hash table for a type isomorphic to TYPE.
6273 If one is found, return it. Otherwise return 0. */
6275 tree
6276 type_hash_lookup (hashval_t hashcode, tree type)
6278 struct type_hash *h, in;
6280 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6281 must call that routine before comparing TYPE_ALIGNs. */
6282 layout_type (type);
6284 in.hash = hashcode;
6285 in.type = type;
6287 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6288 hashcode);
6289 if (h)
6290 return h->type;
6291 return NULL_TREE;
6294 /* Add an entry to the type-hash-table
6295 for a type TYPE whose hash code is HASHCODE. */
6297 void
6298 type_hash_add (hashval_t hashcode, tree type)
6300 struct type_hash *h;
6301 void **loc;
6303 h = ggc_alloc_type_hash ();
6304 h->hash = hashcode;
6305 h->type = type;
6306 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6307 *loc = (void *)h;
6310 /* Given TYPE, and HASHCODE its hash code, return the canonical
6311 object for an identical type if one already exists.
6312 Otherwise, return TYPE, and record it as the canonical object.
6314 To use this function, first create a type of the sort you want.
6315 Then compute its hash code from the fields of the type that
6316 make it different from other similar types.
6317 Then call this function and use the value. */
6319 tree
6320 type_hash_canon (unsigned int hashcode, tree type)
6322 tree t1;
6324 /* The hash table only contains main variants, so ensure that's what we're
6325 being passed. */
6326 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6328 /* See if the type is in the hash table already. If so, return it.
6329 Otherwise, add the type. */
6330 t1 = type_hash_lookup (hashcode, type);
6331 if (t1 != 0)
6333 if (GATHER_STATISTICS)
6335 tree_code_counts[(int) TREE_CODE (type)]--;
6336 tree_node_counts[(int) t_kind]--;
6337 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6339 return t1;
6341 else
6343 type_hash_add (hashcode, type);
6344 return type;
6348 /* See if the data pointed to by the type hash table is marked. We consider
6349 it marked if the type is marked or if a debug type number or symbol
6350 table entry has been made for the type. */
6352 static int
6353 type_hash_marked_p (const void *p)
6355 const_tree const type = ((const struct type_hash *) p)->type;
6357 return ggc_marked_p (type);
6360 static void
6361 print_type_hash_statistics (void)
6363 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6364 (long) htab_size (type_hash_table),
6365 (long) htab_elements (type_hash_table),
6366 htab_collisions (type_hash_table));
6369 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6370 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6371 by adding the hash codes of the individual attributes. */
6373 static unsigned int
6374 attribute_hash_list (const_tree list, hashval_t hashcode)
6376 const_tree tail;
6378 for (tail = list; tail; tail = TREE_CHAIN (tail))
6379 /* ??? Do we want to add in TREE_VALUE too? */
6380 hashcode = iterative_hash_object
6381 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6382 return hashcode;
6385 /* Given two lists of attributes, return true if list l2 is
6386 equivalent to l1. */
6389 attribute_list_equal (const_tree l1, const_tree l2)
6391 if (l1 == l2)
6392 return 1;
6394 return attribute_list_contained (l1, l2)
6395 && attribute_list_contained (l2, l1);
6398 /* Given two lists of attributes, return true if list L2 is
6399 completely contained within L1. */
6400 /* ??? This would be faster if attribute names were stored in a canonicalized
6401 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6402 must be used to show these elements are equivalent (which they are). */
6403 /* ??? It's not clear that attributes with arguments will always be handled
6404 correctly. */
6407 attribute_list_contained (const_tree l1, const_tree l2)
6409 const_tree t1, t2;
6411 /* First check the obvious, maybe the lists are identical. */
6412 if (l1 == l2)
6413 return 1;
6415 /* Maybe the lists are similar. */
6416 for (t1 = l1, t2 = l2;
6417 t1 != 0 && t2 != 0
6418 && get_attribute_name (t1) == get_attribute_name (t2)
6419 && TREE_VALUE (t1) == TREE_VALUE (t2);
6420 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6423 /* Maybe the lists are equal. */
6424 if (t1 == 0 && t2 == 0)
6425 return 1;
6427 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6429 const_tree attr;
6430 /* This CONST_CAST is okay because lookup_attribute does not
6431 modify its argument and the return value is assigned to a
6432 const_tree. */
6433 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6434 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6435 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6438 if (attr == NULL_TREE)
6439 return 0;
6442 return 1;
6445 /* Given two lists of types
6446 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6447 return 1 if the lists contain the same types in the same order.
6448 Also, the TREE_PURPOSEs must match. */
6451 type_list_equal (const_tree l1, const_tree l2)
6453 const_tree t1, t2;
6455 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6456 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6457 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6458 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6459 && (TREE_TYPE (TREE_PURPOSE (t1))
6460 == TREE_TYPE (TREE_PURPOSE (t2))))))
6461 return 0;
6463 return t1 == t2;
6466 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6467 given by TYPE. If the argument list accepts variable arguments,
6468 then this function counts only the ordinary arguments. */
6471 type_num_arguments (const_tree type)
6473 int i = 0;
6474 tree t;
6476 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6477 /* If the function does not take a variable number of arguments,
6478 the last element in the list will have type `void'. */
6479 if (VOID_TYPE_P (TREE_VALUE (t)))
6480 break;
6481 else
6482 ++i;
6484 return i;
6487 /* Nonzero if integer constants T1 and T2
6488 represent the same constant value. */
6491 tree_int_cst_equal (const_tree t1, const_tree t2)
6493 if (t1 == t2)
6494 return 1;
6496 if (t1 == 0 || t2 == 0)
6497 return 0;
6499 if (TREE_CODE (t1) == INTEGER_CST
6500 && TREE_CODE (t2) == INTEGER_CST
6501 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6502 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6503 return 1;
6505 return 0;
6508 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6509 The precise way of comparison depends on their data type. */
6512 tree_int_cst_lt (const_tree t1, const_tree t2)
6514 if (t1 == t2)
6515 return 0;
6517 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6519 int t1_sgn = tree_int_cst_sgn (t1);
6520 int t2_sgn = tree_int_cst_sgn (t2);
6522 if (t1_sgn < t2_sgn)
6523 return 1;
6524 else if (t1_sgn > t2_sgn)
6525 return 0;
6526 /* Otherwise, both are non-negative, so we compare them as
6527 unsigned just in case one of them would overflow a signed
6528 type. */
6530 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6531 return INT_CST_LT (t1, t2);
6533 return INT_CST_LT_UNSIGNED (t1, t2);
6536 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6539 tree_int_cst_compare (const_tree t1, const_tree t2)
6541 if (tree_int_cst_lt (t1, t2))
6542 return -1;
6543 else if (tree_int_cst_lt (t2, t1))
6544 return 1;
6545 else
6546 return 0;
6549 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6550 the host. If POS is zero, the value can be represented in a single
6551 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6552 be represented in a single unsigned HOST_WIDE_INT. */
6555 host_integerp (const_tree t, int pos)
6557 if (t == NULL_TREE)
6558 return 0;
6560 return (TREE_CODE (t) == INTEGER_CST
6561 && ((TREE_INT_CST_HIGH (t) == 0
6562 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6563 || (! pos && TREE_INT_CST_HIGH (t) == -1
6564 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6565 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6566 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6569 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6570 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6571 be non-negative. We must be able to satisfy the above conditions. */
6573 HOST_WIDE_INT
6574 tree_low_cst (const_tree t, int pos)
6576 gcc_assert (host_integerp (t, pos));
6577 return TREE_INT_CST_LOW (t);
6580 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6581 kind INTEGER_CST. This makes sure to properly sign-extend the
6582 constant. */
6584 HOST_WIDE_INT
6585 size_low_cst (const_tree t)
6587 double_int d = tree_to_double_int (t);
6588 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6591 /* Return the most significant (sign) bit of T. */
6594 tree_int_cst_sign_bit (const_tree t)
6596 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6597 unsigned HOST_WIDE_INT w;
6599 if (bitno < HOST_BITS_PER_WIDE_INT)
6600 w = TREE_INT_CST_LOW (t);
6601 else
6603 w = TREE_INT_CST_HIGH (t);
6604 bitno -= HOST_BITS_PER_WIDE_INT;
6607 return (w >> bitno) & 1;
6610 /* Return an indication of the sign of the integer constant T.
6611 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6612 Note that -1 will never be returned if T's type is unsigned. */
6615 tree_int_cst_sgn (const_tree t)
6617 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6618 return 0;
6619 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6620 return 1;
6621 else if (TREE_INT_CST_HIGH (t) < 0)
6622 return -1;
6623 else
6624 return 1;
6627 /* Return the minimum number of bits needed to represent VALUE in a
6628 signed or unsigned type, UNSIGNEDP says which. */
6630 unsigned int
6631 tree_int_cst_min_precision (tree value, bool unsignedp)
6633 int log;
6635 /* If the value is negative, compute its negative minus 1. The latter
6636 adjustment is because the absolute value of the largest negative value
6637 is one larger than the largest positive value. This is equivalent to
6638 a bit-wise negation, so use that operation instead. */
6640 if (tree_int_cst_sgn (value) < 0)
6641 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6643 /* Return the number of bits needed, taking into account the fact
6644 that we need one more bit for a signed than unsigned type. */
6646 if (integer_zerop (value))
6647 log = 0;
6648 else
6649 log = tree_floor_log2 (value);
6651 return log + 1 + !unsignedp;
6654 /* Compare two constructor-element-type constants. Return 1 if the lists
6655 are known to be equal; otherwise return 0. */
6658 simple_cst_list_equal (const_tree l1, const_tree l2)
6660 while (l1 != NULL_TREE && l2 != NULL_TREE)
6662 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6663 return 0;
6665 l1 = TREE_CHAIN (l1);
6666 l2 = TREE_CHAIN (l2);
6669 return l1 == l2;
6672 /* Return truthvalue of whether T1 is the same tree structure as T2.
6673 Return 1 if they are the same.
6674 Return 0 if they are understandably different.
6675 Return -1 if either contains tree structure not understood by
6676 this function. */
6679 simple_cst_equal (const_tree t1, const_tree t2)
6681 enum tree_code code1, code2;
6682 int cmp;
6683 int i;
6685 if (t1 == t2)
6686 return 1;
6687 if (t1 == 0 || t2 == 0)
6688 return 0;
6690 code1 = TREE_CODE (t1);
6691 code2 = TREE_CODE (t2);
6693 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6695 if (CONVERT_EXPR_CODE_P (code2)
6696 || code2 == NON_LVALUE_EXPR)
6697 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6698 else
6699 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6702 else if (CONVERT_EXPR_CODE_P (code2)
6703 || code2 == NON_LVALUE_EXPR)
6704 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6706 if (code1 != code2)
6707 return 0;
6709 switch (code1)
6711 case INTEGER_CST:
6712 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6713 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6715 case REAL_CST:
6716 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6718 case FIXED_CST:
6719 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6721 case STRING_CST:
6722 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6723 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6724 TREE_STRING_LENGTH (t1)));
6726 case CONSTRUCTOR:
6728 unsigned HOST_WIDE_INT idx;
6729 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6730 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6732 if (vec_safe_length (v1) != vec_safe_length (v2))
6733 return false;
6735 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6736 /* ??? Should we handle also fields here? */
6737 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6738 return false;
6739 return true;
6742 case SAVE_EXPR:
6743 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6745 case CALL_EXPR:
6746 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6747 if (cmp <= 0)
6748 return cmp;
6749 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6750 return 0;
6752 const_tree arg1, arg2;
6753 const_call_expr_arg_iterator iter1, iter2;
6754 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6755 arg2 = first_const_call_expr_arg (t2, &iter2);
6756 arg1 && arg2;
6757 arg1 = next_const_call_expr_arg (&iter1),
6758 arg2 = next_const_call_expr_arg (&iter2))
6760 cmp = simple_cst_equal (arg1, arg2);
6761 if (cmp <= 0)
6762 return cmp;
6764 return arg1 == arg2;
6767 case TARGET_EXPR:
6768 /* Special case: if either target is an unallocated VAR_DECL,
6769 it means that it's going to be unified with whatever the
6770 TARGET_EXPR is really supposed to initialize, so treat it
6771 as being equivalent to anything. */
6772 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6773 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6774 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6775 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6776 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6777 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6778 cmp = 1;
6779 else
6780 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6782 if (cmp <= 0)
6783 return cmp;
6785 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6787 case WITH_CLEANUP_EXPR:
6788 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6789 if (cmp <= 0)
6790 return cmp;
6792 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6794 case COMPONENT_REF:
6795 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6796 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6798 return 0;
6800 case VAR_DECL:
6801 case PARM_DECL:
6802 case CONST_DECL:
6803 case FUNCTION_DECL:
6804 return 0;
6806 default:
6807 break;
6810 /* This general rule works for most tree codes. All exceptions should be
6811 handled above. If this is a language-specific tree code, we can't
6812 trust what might be in the operand, so say we don't know
6813 the situation. */
6814 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6815 return -1;
6817 switch (TREE_CODE_CLASS (code1))
6819 case tcc_unary:
6820 case tcc_binary:
6821 case tcc_comparison:
6822 case tcc_expression:
6823 case tcc_reference:
6824 case tcc_statement:
6825 cmp = 1;
6826 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6828 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6829 if (cmp <= 0)
6830 return cmp;
6833 return cmp;
6835 default:
6836 return -1;
6840 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6841 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6842 than U, respectively. */
6845 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6847 if (tree_int_cst_sgn (t) < 0)
6848 return -1;
6849 else if (TREE_INT_CST_HIGH (t) != 0)
6850 return 1;
6851 else if (TREE_INT_CST_LOW (t) == u)
6852 return 0;
6853 else if (TREE_INT_CST_LOW (t) < u)
6854 return -1;
6855 else
6856 return 1;
6859 /* Return true if SIZE represents a constant size that is in bounds of
6860 what the middle-end and the backend accepts (covering not more than
6861 half of the address-space). */
6863 bool
6864 valid_constant_size_p (const_tree size)
6866 if (! host_integerp (size, 1)
6867 || TREE_OVERFLOW (size)
6868 || tree_int_cst_sign_bit (size) != 0)
6869 return false;
6870 return true;
6873 /* Return true if CODE represents an associative tree code. Otherwise
6874 return false. */
6875 bool
6876 associative_tree_code (enum tree_code code)
6878 switch (code)
6880 case BIT_IOR_EXPR:
6881 case BIT_AND_EXPR:
6882 case BIT_XOR_EXPR:
6883 case PLUS_EXPR:
6884 case MULT_EXPR:
6885 case MIN_EXPR:
6886 case MAX_EXPR:
6887 return true;
6889 default:
6890 break;
6892 return false;
6895 /* Return true if CODE represents a commutative tree code. Otherwise
6896 return false. */
6897 bool
6898 commutative_tree_code (enum tree_code code)
6900 switch (code)
6902 case PLUS_EXPR:
6903 case MULT_EXPR:
6904 case MULT_HIGHPART_EXPR:
6905 case MIN_EXPR:
6906 case MAX_EXPR:
6907 case BIT_IOR_EXPR:
6908 case BIT_XOR_EXPR:
6909 case BIT_AND_EXPR:
6910 case NE_EXPR:
6911 case EQ_EXPR:
6912 case UNORDERED_EXPR:
6913 case ORDERED_EXPR:
6914 case UNEQ_EXPR:
6915 case LTGT_EXPR:
6916 case TRUTH_AND_EXPR:
6917 case TRUTH_XOR_EXPR:
6918 case TRUTH_OR_EXPR:
6919 case WIDEN_MULT_EXPR:
6920 case VEC_WIDEN_MULT_HI_EXPR:
6921 case VEC_WIDEN_MULT_LO_EXPR:
6922 case VEC_WIDEN_MULT_EVEN_EXPR:
6923 case VEC_WIDEN_MULT_ODD_EXPR:
6924 return true;
6926 default:
6927 break;
6929 return false;
6932 /* Return true if CODE represents a ternary tree code for which the
6933 first two operands are commutative. Otherwise return false. */
6934 bool
6935 commutative_ternary_tree_code (enum tree_code code)
6937 switch (code)
6939 case WIDEN_MULT_PLUS_EXPR:
6940 case WIDEN_MULT_MINUS_EXPR:
6941 return true;
6943 default:
6944 break;
6946 return false;
6949 /* Generate a hash value for an expression. This can be used iteratively
6950 by passing a previous result as the VAL argument.
6952 This function is intended to produce the same hash for expressions which
6953 would compare equal using operand_equal_p. */
6955 hashval_t
6956 iterative_hash_expr (const_tree t, hashval_t val)
6958 int i;
6959 enum tree_code code;
6960 char tclass;
6962 if (t == NULL_TREE)
6963 return iterative_hash_hashval_t (0, val);
6965 code = TREE_CODE (t);
6967 switch (code)
6969 /* Alas, constants aren't shared, so we can't rely on pointer
6970 identity. */
6971 case INTEGER_CST:
6972 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
6973 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
6974 case REAL_CST:
6976 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
6978 return iterative_hash_hashval_t (val2, val);
6980 case FIXED_CST:
6982 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
6984 return iterative_hash_hashval_t (val2, val);
6986 case STRING_CST:
6987 return iterative_hash (TREE_STRING_POINTER (t),
6988 TREE_STRING_LENGTH (t), val);
6989 case COMPLEX_CST:
6990 val = iterative_hash_expr (TREE_REALPART (t), val);
6991 return iterative_hash_expr (TREE_IMAGPART (t), val);
6992 case VECTOR_CST:
6994 unsigned i;
6995 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
6996 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
6997 return val;
6999 case SSA_NAME:
7000 /* We can just compare by pointer. */
7001 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7002 case PLACEHOLDER_EXPR:
7003 /* The node itself doesn't matter. */
7004 return val;
7005 case TREE_LIST:
7006 /* A list of expressions, for a CALL_EXPR or as the elements of a
7007 VECTOR_CST. */
7008 for (; t; t = TREE_CHAIN (t))
7009 val = iterative_hash_expr (TREE_VALUE (t), val);
7010 return val;
7011 case CONSTRUCTOR:
7013 unsigned HOST_WIDE_INT idx;
7014 tree field, value;
7015 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7017 val = iterative_hash_expr (field, val);
7018 val = iterative_hash_expr (value, val);
7020 return val;
7022 case FUNCTION_DECL:
7023 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7024 Otherwise nodes that compare equal according to operand_equal_p might
7025 get different hash codes. However, don't do this for machine specific
7026 or front end builtins, since the function code is overloaded in those
7027 cases. */
7028 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7029 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7031 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7032 code = TREE_CODE (t);
7034 /* FALL THROUGH */
7035 default:
7036 tclass = TREE_CODE_CLASS (code);
7038 if (tclass == tcc_declaration)
7040 /* DECL's have a unique ID */
7041 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7043 else
7045 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7047 val = iterative_hash_object (code, val);
7049 /* Don't hash the type, that can lead to having nodes which
7050 compare equal according to operand_equal_p, but which
7051 have different hash codes. */
7052 if (CONVERT_EXPR_CODE_P (code)
7053 || code == NON_LVALUE_EXPR)
7055 /* Make sure to include signness in the hash computation. */
7056 val += TYPE_UNSIGNED (TREE_TYPE (t));
7057 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7060 else if (commutative_tree_code (code))
7062 /* It's a commutative expression. We want to hash it the same
7063 however it appears. We do this by first hashing both operands
7064 and then rehashing based on the order of their independent
7065 hashes. */
7066 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7067 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7068 hashval_t t;
7070 if (one > two)
7071 t = one, one = two, two = t;
7073 val = iterative_hash_hashval_t (one, val);
7074 val = iterative_hash_hashval_t (two, val);
7076 else
7077 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7078 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7080 return val;
7084 /* Generate a hash value for a pair of expressions. This can be used
7085 iteratively by passing a previous result as the VAL argument.
7087 The same hash value is always returned for a given pair of expressions,
7088 regardless of the order in which they are presented. This is useful in
7089 hashing the operands of commutative functions. */
7091 hashval_t
7092 iterative_hash_exprs_commutative (const_tree t1,
7093 const_tree t2, hashval_t val)
7095 hashval_t one = iterative_hash_expr (t1, 0);
7096 hashval_t two = iterative_hash_expr (t2, 0);
7097 hashval_t t;
7099 if (one > two)
7100 t = one, one = two, two = t;
7101 val = iterative_hash_hashval_t (one, val);
7102 val = iterative_hash_hashval_t (two, val);
7104 return val;
7107 /* Constructors for pointer, array and function types.
7108 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7109 constructed by language-dependent code, not here.) */
7111 /* Construct, lay out and return the type of pointers to TO_TYPE with
7112 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7113 reference all of memory. If such a type has already been
7114 constructed, reuse it. */
7116 tree
7117 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7118 bool can_alias_all)
7120 tree t;
7122 if (to_type == error_mark_node)
7123 return error_mark_node;
7125 /* If the pointed-to type has the may_alias attribute set, force
7126 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7127 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7128 can_alias_all = true;
7130 /* In some cases, languages will have things that aren't a POINTER_TYPE
7131 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7132 In that case, return that type without regard to the rest of our
7133 operands.
7135 ??? This is a kludge, but consistent with the way this function has
7136 always operated and there doesn't seem to be a good way to avoid this
7137 at the moment. */
7138 if (TYPE_POINTER_TO (to_type) != 0
7139 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7140 return TYPE_POINTER_TO (to_type);
7142 /* First, if we already have a type for pointers to TO_TYPE and it's
7143 the proper mode, use it. */
7144 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7145 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7146 return t;
7148 t = make_node (POINTER_TYPE);
7150 TREE_TYPE (t) = to_type;
7151 SET_TYPE_MODE (t, mode);
7152 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7153 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7154 TYPE_POINTER_TO (to_type) = t;
7156 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7157 SET_TYPE_STRUCTURAL_EQUALITY (t);
7158 else if (TYPE_CANONICAL (to_type) != to_type)
7159 TYPE_CANONICAL (t)
7160 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7161 mode, can_alias_all);
7163 /* Lay out the type. This function has many callers that are concerned
7164 with expression-construction, and this simplifies them all. */
7165 layout_type (t);
7167 return t;
7170 /* By default build pointers in ptr_mode. */
7172 tree
7173 build_pointer_type (tree to_type)
7175 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7176 : TYPE_ADDR_SPACE (to_type);
7177 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7178 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7181 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7183 tree
7184 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7185 bool can_alias_all)
7187 tree t;
7189 if (to_type == error_mark_node)
7190 return error_mark_node;
7192 /* If the pointed-to type has the may_alias attribute set, force
7193 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7194 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7195 can_alias_all = true;
7197 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7198 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7199 In that case, return that type without regard to the rest of our
7200 operands.
7202 ??? This is a kludge, but consistent with the way this function has
7203 always operated and there doesn't seem to be a good way to avoid this
7204 at the moment. */
7205 if (TYPE_REFERENCE_TO (to_type) != 0
7206 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7207 return TYPE_REFERENCE_TO (to_type);
7209 /* First, if we already have a type for pointers to TO_TYPE and it's
7210 the proper mode, use it. */
7211 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7212 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7213 return t;
7215 t = make_node (REFERENCE_TYPE);
7217 TREE_TYPE (t) = to_type;
7218 SET_TYPE_MODE (t, mode);
7219 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7220 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7221 TYPE_REFERENCE_TO (to_type) = t;
7223 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7224 SET_TYPE_STRUCTURAL_EQUALITY (t);
7225 else if (TYPE_CANONICAL (to_type) != to_type)
7226 TYPE_CANONICAL (t)
7227 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7228 mode, can_alias_all);
7230 layout_type (t);
7232 return t;
7236 /* Build the node for the type of references-to-TO_TYPE by default
7237 in ptr_mode. */
7239 tree
7240 build_reference_type (tree to_type)
7242 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7243 : TYPE_ADDR_SPACE (to_type);
7244 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7245 return build_reference_type_for_mode (to_type, pointer_mode, false);
7248 /* Build a type that is compatible with t but has no cv quals anywhere
7249 in its type, thus
7251 const char *const *const * -> char ***. */
7253 tree
7254 build_type_no_quals (tree t)
7256 switch (TREE_CODE (t))
7258 case POINTER_TYPE:
7259 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7260 TYPE_MODE (t),
7261 TYPE_REF_CAN_ALIAS_ALL (t));
7262 case REFERENCE_TYPE:
7263 return
7264 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7265 TYPE_MODE (t),
7266 TYPE_REF_CAN_ALIAS_ALL (t));
7267 default:
7268 return TYPE_MAIN_VARIANT (t);
7272 #define MAX_INT_CACHED_PREC \
7273 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7274 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7276 /* Builds a signed or unsigned integer type of precision PRECISION.
7277 Used for C bitfields whose precision does not match that of
7278 built-in target types. */
7279 tree
7280 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7281 int unsignedp)
7283 tree itype, ret;
7285 if (unsignedp)
7286 unsignedp = MAX_INT_CACHED_PREC + 1;
7288 if (precision <= MAX_INT_CACHED_PREC)
7290 itype = nonstandard_integer_type_cache[precision + unsignedp];
7291 if (itype)
7292 return itype;
7295 itype = make_node (INTEGER_TYPE);
7296 TYPE_PRECISION (itype) = precision;
7298 if (unsignedp)
7299 fixup_unsigned_type (itype);
7300 else
7301 fixup_signed_type (itype);
7303 ret = itype;
7304 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7305 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7306 if (precision <= MAX_INT_CACHED_PREC)
7307 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7309 return ret;
7312 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7313 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7314 is true, reuse such a type that has already been constructed. */
7316 static tree
7317 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7319 tree itype = make_node (INTEGER_TYPE);
7320 hashval_t hashcode = 0;
7322 TREE_TYPE (itype) = type;
7324 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7325 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7327 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7328 SET_TYPE_MODE (itype, TYPE_MODE (type));
7329 TYPE_SIZE (itype) = TYPE_SIZE (type);
7330 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7331 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7332 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7334 if (!shared)
7335 return itype;
7337 if ((TYPE_MIN_VALUE (itype)
7338 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7339 || (TYPE_MAX_VALUE (itype)
7340 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7342 /* Since we cannot reliably merge this type, we need to compare it using
7343 structural equality checks. */
7344 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7345 return itype;
7348 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7349 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7350 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7351 itype = type_hash_canon (hashcode, itype);
7353 return itype;
7356 /* Wrapper around build_range_type_1 with SHARED set to true. */
7358 tree
7359 build_range_type (tree type, tree lowval, tree highval)
7361 return build_range_type_1 (type, lowval, highval, true);
7364 /* Wrapper around build_range_type_1 with SHARED set to false. */
7366 tree
7367 build_nonshared_range_type (tree type, tree lowval, tree highval)
7369 return build_range_type_1 (type, lowval, highval, false);
7372 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7373 MAXVAL should be the maximum value in the domain
7374 (one less than the length of the array).
7376 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7377 We don't enforce this limit, that is up to caller (e.g. language front end).
7378 The limit exists because the result is a signed type and we don't handle
7379 sizes that use more than one HOST_WIDE_INT. */
7381 tree
7382 build_index_type (tree maxval)
7384 return build_range_type (sizetype, size_zero_node, maxval);
7387 /* Return true if the debug information for TYPE, a subtype, should be emitted
7388 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7389 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7390 debug info and doesn't reflect the source code. */
7392 bool
7393 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7395 tree base_type = TREE_TYPE (type), low, high;
7397 /* Subrange types have a base type which is an integral type. */
7398 if (!INTEGRAL_TYPE_P (base_type))
7399 return false;
7401 /* Get the real bounds of the subtype. */
7402 if (lang_hooks.types.get_subrange_bounds)
7403 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7404 else
7406 low = TYPE_MIN_VALUE (type);
7407 high = TYPE_MAX_VALUE (type);
7410 /* If the type and its base type have the same representation and the same
7411 name, then the type is not a subrange but a copy of the base type. */
7412 if ((TREE_CODE (base_type) == INTEGER_TYPE
7413 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7414 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7415 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7416 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7418 tree type_name = TYPE_NAME (type);
7419 tree base_type_name = TYPE_NAME (base_type);
7421 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7422 type_name = DECL_NAME (type_name);
7424 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7425 base_type_name = DECL_NAME (base_type_name);
7427 if (type_name == base_type_name)
7428 return false;
7431 if (lowval)
7432 *lowval = low;
7433 if (highval)
7434 *highval = high;
7435 return true;
7438 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7439 and number of elements specified by the range of values of INDEX_TYPE.
7440 If SHARED is true, reuse such a type that has already been constructed. */
7442 static tree
7443 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7445 tree t;
7447 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7449 error ("arrays of functions are not meaningful");
7450 elt_type = integer_type_node;
7453 t = make_node (ARRAY_TYPE);
7454 TREE_TYPE (t) = elt_type;
7455 TYPE_DOMAIN (t) = index_type;
7456 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7457 layout_type (t);
7459 /* If the element type is incomplete at this point we get marked for
7460 structural equality. Do not record these types in the canonical
7461 type hashtable. */
7462 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7463 return t;
7465 if (shared)
7467 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7468 if (index_type)
7469 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7470 t = type_hash_canon (hashcode, t);
7473 if (TYPE_CANONICAL (t) == t)
7475 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7476 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7477 SET_TYPE_STRUCTURAL_EQUALITY (t);
7478 else if (TYPE_CANONICAL (elt_type) != elt_type
7479 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7480 TYPE_CANONICAL (t)
7481 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7482 index_type
7483 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7484 shared);
7487 return t;
7490 /* Wrapper around build_array_type_1 with SHARED set to true. */
7492 tree
7493 build_array_type (tree elt_type, tree index_type)
7495 return build_array_type_1 (elt_type, index_type, true);
7498 /* Wrapper around build_array_type_1 with SHARED set to false. */
7500 tree
7501 build_nonshared_array_type (tree elt_type, tree index_type)
7503 return build_array_type_1 (elt_type, index_type, false);
7506 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7507 sizetype. */
7509 tree
7510 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7512 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7515 /* Recursively examines the array elements of TYPE, until a non-array
7516 element type is found. */
7518 tree
7519 strip_array_types (tree type)
7521 while (TREE_CODE (type) == ARRAY_TYPE)
7522 type = TREE_TYPE (type);
7524 return type;
7527 /* Computes the canonical argument types from the argument type list
7528 ARGTYPES.
7530 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7531 on entry to this function, or if any of the ARGTYPES are
7532 structural.
7534 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7535 true on entry to this function, or if any of the ARGTYPES are
7536 non-canonical.
7538 Returns a canonical argument list, which may be ARGTYPES when the
7539 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7540 true) or would not differ from ARGTYPES. */
7542 static tree
7543 maybe_canonicalize_argtypes(tree argtypes,
7544 bool *any_structural_p,
7545 bool *any_noncanonical_p)
7547 tree arg;
7548 bool any_noncanonical_argtypes_p = false;
7550 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7552 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7553 /* Fail gracefully by stating that the type is structural. */
7554 *any_structural_p = true;
7555 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7556 *any_structural_p = true;
7557 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7558 || TREE_PURPOSE (arg))
7559 /* If the argument has a default argument, we consider it
7560 non-canonical even though the type itself is canonical.
7561 That way, different variants of function and method types
7562 with default arguments will all point to the variant with
7563 no defaults as their canonical type. */
7564 any_noncanonical_argtypes_p = true;
7567 if (*any_structural_p)
7568 return argtypes;
7570 if (any_noncanonical_argtypes_p)
7572 /* Build the canonical list of argument types. */
7573 tree canon_argtypes = NULL_TREE;
7574 bool is_void = false;
7576 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7578 if (arg == void_list_node)
7579 is_void = true;
7580 else
7581 canon_argtypes = tree_cons (NULL_TREE,
7582 TYPE_CANONICAL (TREE_VALUE (arg)),
7583 canon_argtypes);
7586 canon_argtypes = nreverse (canon_argtypes);
7587 if (is_void)
7588 canon_argtypes = chainon (canon_argtypes, void_list_node);
7590 /* There is a non-canonical type. */
7591 *any_noncanonical_p = true;
7592 return canon_argtypes;
7595 /* The canonical argument types are the same as ARGTYPES. */
7596 return argtypes;
7599 /* Construct, lay out and return
7600 the type of functions returning type VALUE_TYPE
7601 given arguments of types ARG_TYPES.
7602 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7603 are data type nodes for the arguments of the function.
7604 If such a type has already been constructed, reuse it. */
7606 tree
7607 build_function_type (tree value_type, tree arg_types)
7609 tree t;
7610 hashval_t hashcode = 0;
7611 bool any_structural_p, any_noncanonical_p;
7612 tree canon_argtypes;
7614 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7616 error ("function return type cannot be function");
7617 value_type = integer_type_node;
7620 /* Make a node of the sort we want. */
7621 t = make_node (FUNCTION_TYPE);
7622 TREE_TYPE (t) = value_type;
7623 TYPE_ARG_TYPES (t) = arg_types;
7625 /* If we already have such a type, use the old one. */
7626 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7627 hashcode = type_hash_list (arg_types, hashcode);
7628 t = type_hash_canon (hashcode, t);
7630 /* Set up the canonical type. */
7631 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7632 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7633 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7634 &any_structural_p,
7635 &any_noncanonical_p);
7636 if (any_structural_p)
7637 SET_TYPE_STRUCTURAL_EQUALITY (t);
7638 else if (any_noncanonical_p)
7639 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7640 canon_argtypes);
7642 if (!COMPLETE_TYPE_P (t))
7643 layout_type (t);
7644 return t;
7647 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7648 return value if SKIP_RETURN is true. */
7650 static tree
7651 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7652 bool skip_return)
7654 tree new_type = NULL;
7655 tree args, new_args = NULL, t;
7656 tree new_reversed;
7657 int i = 0;
7659 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7660 args = TREE_CHAIN (args), i++)
7661 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7662 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7664 new_reversed = nreverse (new_args);
7665 if (args)
7667 if (new_reversed)
7668 TREE_CHAIN (new_args) = void_list_node;
7669 else
7670 new_reversed = void_list_node;
7673 /* Use copy_node to preserve as much as possible from original type
7674 (debug info, attribute lists etc.)
7675 Exception is METHOD_TYPEs must have THIS argument.
7676 When we are asked to remove it, we need to build new FUNCTION_TYPE
7677 instead. */
7678 if (TREE_CODE (orig_type) != METHOD_TYPE
7679 || !args_to_skip
7680 || !bitmap_bit_p (args_to_skip, 0))
7682 new_type = build_distinct_type_copy (orig_type);
7683 TYPE_ARG_TYPES (new_type) = new_reversed;
7685 else
7687 new_type
7688 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7689 new_reversed));
7690 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7693 if (skip_return)
7694 TREE_TYPE (new_type) = void_type_node;
7696 /* This is a new type, not a copy of an old type. Need to reassociate
7697 variants. We can handle everything except the main variant lazily. */
7698 t = TYPE_MAIN_VARIANT (orig_type);
7699 if (t != orig_type)
7701 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7702 TYPE_MAIN_VARIANT (new_type) = t;
7703 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7704 TYPE_NEXT_VARIANT (t) = new_type;
7706 else
7708 TYPE_MAIN_VARIANT (new_type) = new_type;
7709 TYPE_NEXT_VARIANT (new_type) = NULL;
7712 return new_type;
7715 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7716 return value if SKIP_RETURN is true.
7718 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7719 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7720 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7722 tree
7723 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7724 bool skip_return)
7726 tree new_decl = copy_node (orig_decl);
7727 tree new_type;
7729 new_type = TREE_TYPE (orig_decl);
7730 if (prototype_p (new_type)
7731 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7732 new_type
7733 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7734 TREE_TYPE (new_decl) = new_type;
7736 /* For declarations setting DECL_VINDEX (i.e. methods)
7737 we expect first argument to be THIS pointer. */
7738 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7739 DECL_VINDEX (new_decl) = NULL_TREE;
7741 /* When signature changes, we need to clear builtin info. */
7742 if (DECL_BUILT_IN (new_decl)
7743 && args_to_skip
7744 && !bitmap_empty_p (args_to_skip))
7746 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7747 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7749 return new_decl;
7752 /* Build a function type. The RETURN_TYPE is the type returned by the
7753 function. If VAARGS is set, no void_type_node is appended to the
7754 the list. ARGP must be always be terminated be a NULL_TREE. */
7756 static tree
7757 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7759 tree t, args, last;
7761 t = va_arg (argp, tree);
7762 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7763 args = tree_cons (NULL_TREE, t, args);
7765 if (vaargs)
7767 last = args;
7768 if (args != NULL_TREE)
7769 args = nreverse (args);
7770 gcc_assert (last != void_list_node);
7772 else if (args == NULL_TREE)
7773 args = void_list_node;
7774 else
7776 last = args;
7777 args = nreverse (args);
7778 TREE_CHAIN (last) = void_list_node;
7780 args = build_function_type (return_type, args);
7782 return args;
7785 /* Build a function type. The RETURN_TYPE is the type returned by the
7786 function. If additional arguments are provided, they are
7787 additional argument types. The list of argument types must always
7788 be terminated by NULL_TREE. */
7790 tree
7791 build_function_type_list (tree return_type, ...)
7793 tree args;
7794 va_list p;
7796 va_start (p, return_type);
7797 args = build_function_type_list_1 (false, return_type, p);
7798 va_end (p);
7799 return args;
7802 /* Build a variable argument function type. The RETURN_TYPE is the
7803 type returned by the function. If additional arguments are provided,
7804 they are additional argument types. The list of argument types must
7805 always be terminated by NULL_TREE. */
7807 tree
7808 build_varargs_function_type_list (tree return_type, ...)
7810 tree args;
7811 va_list p;
7813 va_start (p, return_type);
7814 args = build_function_type_list_1 (true, return_type, p);
7815 va_end (p);
7817 return args;
7820 /* Build a function type. RETURN_TYPE is the type returned by the
7821 function; VAARGS indicates whether the function takes varargs. The
7822 function takes N named arguments, the types of which are provided in
7823 ARG_TYPES. */
7825 static tree
7826 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7827 tree *arg_types)
7829 int i;
7830 tree t = vaargs ? NULL_TREE : void_list_node;
7832 for (i = n - 1; i >= 0; i--)
7833 t = tree_cons (NULL_TREE, arg_types[i], t);
7835 return build_function_type (return_type, t);
7838 /* Build a function type. RETURN_TYPE is the type returned by the
7839 function. The function takes N named arguments, the types of which
7840 are provided in ARG_TYPES. */
7842 tree
7843 build_function_type_array (tree return_type, int n, tree *arg_types)
7845 return build_function_type_array_1 (false, return_type, n, arg_types);
7848 /* Build a variable argument function type. RETURN_TYPE is the type
7849 returned by the function. The function takes N named arguments, the
7850 types of which are provided in ARG_TYPES. */
7852 tree
7853 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7855 return build_function_type_array_1 (true, return_type, n, arg_types);
7858 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7859 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7860 for the method. An implicit additional parameter (of type
7861 pointer-to-BASETYPE) is added to the ARGTYPES. */
7863 tree
7864 build_method_type_directly (tree basetype,
7865 tree rettype,
7866 tree argtypes)
7868 tree t;
7869 tree ptype;
7870 int hashcode = 0;
7871 bool any_structural_p, any_noncanonical_p;
7872 tree canon_argtypes;
7874 /* Make a node of the sort we want. */
7875 t = make_node (METHOD_TYPE);
7877 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7878 TREE_TYPE (t) = rettype;
7879 ptype = build_pointer_type (basetype);
7881 /* The actual arglist for this function includes a "hidden" argument
7882 which is "this". Put it into the list of argument types. */
7883 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7884 TYPE_ARG_TYPES (t) = argtypes;
7886 /* If we already have such a type, use the old one. */
7887 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7888 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7889 hashcode = type_hash_list (argtypes, hashcode);
7890 t = type_hash_canon (hashcode, t);
7892 /* Set up the canonical type. */
7893 any_structural_p
7894 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7895 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7896 any_noncanonical_p
7897 = (TYPE_CANONICAL (basetype) != basetype
7898 || TYPE_CANONICAL (rettype) != rettype);
7899 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7900 &any_structural_p,
7901 &any_noncanonical_p);
7902 if (any_structural_p)
7903 SET_TYPE_STRUCTURAL_EQUALITY (t);
7904 else if (any_noncanonical_p)
7905 TYPE_CANONICAL (t)
7906 = build_method_type_directly (TYPE_CANONICAL (basetype),
7907 TYPE_CANONICAL (rettype),
7908 canon_argtypes);
7909 if (!COMPLETE_TYPE_P (t))
7910 layout_type (t);
7912 return t;
7915 /* Construct, lay out and return the type of methods belonging to class
7916 BASETYPE and whose arguments and values are described by TYPE.
7917 If that type exists already, reuse it.
7918 TYPE must be a FUNCTION_TYPE node. */
7920 tree
7921 build_method_type (tree basetype, tree type)
7923 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7925 return build_method_type_directly (basetype,
7926 TREE_TYPE (type),
7927 TYPE_ARG_TYPES (type));
7930 /* Construct, lay out and return the type of offsets to a value
7931 of type TYPE, within an object of type BASETYPE.
7932 If a suitable offset type exists already, reuse it. */
7934 tree
7935 build_offset_type (tree basetype, tree type)
7937 tree t;
7938 hashval_t hashcode = 0;
7940 /* Make a node of the sort we want. */
7941 t = make_node (OFFSET_TYPE);
7943 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7944 TREE_TYPE (t) = type;
7946 /* If we already have such a type, use the old one. */
7947 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7948 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7949 t = type_hash_canon (hashcode, t);
7951 if (!COMPLETE_TYPE_P (t))
7952 layout_type (t);
7954 if (TYPE_CANONICAL (t) == t)
7956 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7957 || TYPE_STRUCTURAL_EQUALITY_P (type))
7958 SET_TYPE_STRUCTURAL_EQUALITY (t);
7959 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7960 || TYPE_CANONICAL (type) != type)
7961 TYPE_CANONICAL (t)
7962 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7963 TYPE_CANONICAL (type));
7966 return t;
7969 /* Create a complex type whose components are COMPONENT_TYPE. */
7971 tree
7972 build_complex_type (tree component_type)
7974 tree t;
7975 hashval_t hashcode;
7977 gcc_assert (INTEGRAL_TYPE_P (component_type)
7978 || SCALAR_FLOAT_TYPE_P (component_type)
7979 || FIXED_POINT_TYPE_P (component_type));
7981 /* Make a node of the sort we want. */
7982 t = make_node (COMPLEX_TYPE);
7984 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
7986 /* If we already have such a type, use the old one. */
7987 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
7988 t = type_hash_canon (hashcode, t);
7990 if (!COMPLETE_TYPE_P (t))
7991 layout_type (t);
7993 if (TYPE_CANONICAL (t) == t)
7995 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
7996 SET_TYPE_STRUCTURAL_EQUALITY (t);
7997 else if (TYPE_CANONICAL (component_type) != component_type)
7998 TYPE_CANONICAL (t)
7999 = build_complex_type (TYPE_CANONICAL (component_type));
8002 /* We need to create a name, since complex is a fundamental type. */
8003 if (! TYPE_NAME (t))
8005 const char *name;
8006 if (component_type == char_type_node)
8007 name = "complex char";
8008 else if (component_type == signed_char_type_node)
8009 name = "complex signed char";
8010 else if (component_type == unsigned_char_type_node)
8011 name = "complex unsigned char";
8012 else if (component_type == short_integer_type_node)
8013 name = "complex short int";
8014 else if (component_type == short_unsigned_type_node)
8015 name = "complex short unsigned int";
8016 else if (component_type == integer_type_node)
8017 name = "complex int";
8018 else if (component_type == unsigned_type_node)
8019 name = "complex unsigned int";
8020 else if (component_type == long_integer_type_node)
8021 name = "complex long int";
8022 else if (component_type == long_unsigned_type_node)
8023 name = "complex long unsigned int";
8024 else if (component_type == long_long_integer_type_node)
8025 name = "complex long long int";
8026 else if (component_type == long_long_unsigned_type_node)
8027 name = "complex long long unsigned int";
8028 else
8029 name = 0;
8031 if (name != 0)
8032 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8033 get_identifier (name), t);
8036 return build_qualified_type (t, TYPE_QUALS (component_type));
8039 /* If TYPE is a real or complex floating-point type and the target
8040 does not directly support arithmetic on TYPE then return the wider
8041 type to be used for arithmetic on TYPE. Otherwise, return
8042 NULL_TREE. */
8044 tree
8045 excess_precision_type (tree type)
8047 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8049 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8050 switch (TREE_CODE (type))
8052 case REAL_TYPE:
8053 switch (flt_eval_method)
8055 case 1:
8056 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8057 return double_type_node;
8058 break;
8059 case 2:
8060 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8061 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8062 return long_double_type_node;
8063 break;
8064 default:
8065 gcc_unreachable ();
8067 break;
8068 case COMPLEX_TYPE:
8069 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8070 return NULL_TREE;
8071 switch (flt_eval_method)
8073 case 1:
8074 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8075 return complex_double_type_node;
8076 break;
8077 case 2:
8078 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8079 || (TYPE_MODE (TREE_TYPE (type))
8080 == TYPE_MODE (double_type_node)))
8081 return complex_long_double_type_node;
8082 break;
8083 default:
8084 gcc_unreachable ();
8086 break;
8087 default:
8088 break;
8091 return NULL_TREE;
8094 /* Return OP, stripped of any conversions to wider types as much as is safe.
8095 Converting the value back to OP's type makes a value equivalent to OP.
8097 If FOR_TYPE is nonzero, we return a value which, if converted to
8098 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8100 OP must have integer, real or enumeral type. Pointers are not allowed!
8102 There are some cases where the obvious value we could return
8103 would regenerate to OP if converted to OP's type,
8104 but would not extend like OP to wider types.
8105 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8106 For example, if OP is (unsigned short)(signed char)-1,
8107 we avoid returning (signed char)-1 if FOR_TYPE is int,
8108 even though extending that to an unsigned short would regenerate OP,
8109 since the result of extending (signed char)-1 to (int)
8110 is different from (int) OP. */
8112 tree
8113 get_unwidened (tree op, tree for_type)
8115 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8116 tree type = TREE_TYPE (op);
8117 unsigned final_prec
8118 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8119 int uns
8120 = (for_type != 0 && for_type != type
8121 && final_prec > TYPE_PRECISION (type)
8122 && TYPE_UNSIGNED (type));
8123 tree win = op;
8125 while (CONVERT_EXPR_P (op))
8127 int bitschange;
8129 /* TYPE_PRECISION on vector types has different meaning
8130 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8131 so avoid them here. */
8132 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8133 break;
8135 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8136 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8138 /* Truncations are many-one so cannot be removed.
8139 Unless we are later going to truncate down even farther. */
8140 if (bitschange < 0
8141 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8142 break;
8144 /* See what's inside this conversion. If we decide to strip it,
8145 we will set WIN. */
8146 op = TREE_OPERAND (op, 0);
8148 /* If we have not stripped any zero-extensions (uns is 0),
8149 we can strip any kind of extension.
8150 If we have previously stripped a zero-extension,
8151 only zero-extensions can safely be stripped.
8152 Any extension can be stripped if the bits it would produce
8153 are all going to be discarded later by truncating to FOR_TYPE. */
8155 if (bitschange > 0)
8157 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8158 win = op;
8159 /* TYPE_UNSIGNED says whether this is a zero-extension.
8160 Let's avoid computing it if it does not affect WIN
8161 and if UNS will not be needed again. */
8162 if ((uns
8163 || CONVERT_EXPR_P (op))
8164 && TYPE_UNSIGNED (TREE_TYPE (op)))
8166 uns = 1;
8167 win = op;
8172 /* If we finally reach a constant see if it fits in for_type and
8173 in that case convert it. */
8174 if (for_type
8175 && TREE_CODE (win) == INTEGER_CST
8176 && TREE_TYPE (win) != for_type
8177 && int_fits_type_p (win, for_type))
8178 win = fold_convert (for_type, win);
8180 return win;
8183 /* Return OP or a simpler expression for a narrower value
8184 which can be sign-extended or zero-extended to give back OP.
8185 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8186 or 0 if the value should be sign-extended. */
8188 tree
8189 get_narrower (tree op, int *unsignedp_ptr)
8191 int uns = 0;
8192 int first = 1;
8193 tree win = op;
8194 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8196 while (TREE_CODE (op) == NOP_EXPR)
8198 int bitschange
8199 = (TYPE_PRECISION (TREE_TYPE (op))
8200 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8202 /* Truncations are many-one so cannot be removed. */
8203 if (bitschange < 0)
8204 break;
8206 /* See what's inside this conversion. If we decide to strip it,
8207 we will set WIN. */
8209 if (bitschange > 0)
8211 op = TREE_OPERAND (op, 0);
8212 /* An extension: the outermost one can be stripped,
8213 but remember whether it is zero or sign extension. */
8214 if (first)
8215 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8216 /* Otherwise, if a sign extension has been stripped,
8217 only sign extensions can now be stripped;
8218 if a zero extension has been stripped, only zero-extensions. */
8219 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8220 break;
8221 first = 0;
8223 else /* bitschange == 0 */
8225 /* A change in nominal type can always be stripped, but we must
8226 preserve the unsignedness. */
8227 if (first)
8228 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8229 first = 0;
8230 op = TREE_OPERAND (op, 0);
8231 /* Keep trying to narrow, but don't assign op to win if it
8232 would turn an integral type into something else. */
8233 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8234 continue;
8237 win = op;
8240 if (TREE_CODE (op) == COMPONENT_REF
8241 /* Since type_for_size always gives an integer type. */
8242 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8243 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8244 /* Ensure field is laid out already. */
8245 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8246 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8248 unsigned HOST_WIDE_INT innerprec
8249 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8250 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8251 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8252 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8254 /* We can get this structure field in a narrower type that fits it,
8255 but the resulting extension to its nominal type (a fullword type)
8256 must satisfy the same conditions as for other extensions.
8258 Do this only for fields that are aligned (not bit-fields),
8259 because when bit-field insns will be used there is no
8260 advantage in doing this. */
8262 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8263 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8264 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8265 && type != 0)
8267 if (first)
8268 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8269 win = fold_convert (type, op);
8273 *unsignedp_ptr = uns;
8274 return win;
8277 /* Returns true if integer constant C has a value that is permissible
8278 for type TYPE (an INTEGER_TYPE). */
8280 bool
8281 int_fits_type_p (const_tree c, const_tree type)
8283 tree type_low_bound, type_high_bound;
8284 bool ok_for_low_bound, ok_for_high_bound, unsc;
8285 double_int dc, dd;
8287 dc = tree_to_double_int (c);
8288 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8290 retry:
8291 type_low_bound = TYPE_MIN_VALUE (type);
8292 type_high_bound = TYPE_MAX_VALUE (type);
8294 /* If at least one bound of the type is a constant integer, we can check
8295 ourselves and maybe make a decision. If no such decision is possible, but
8296 this type is a subtype, try checking against that. Otherwise, use
8297 double_int_fits_to_tree_p, which checks against the precision.
8299 Compute the status for each possibly constant bound, and return if we see
8300 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8301 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8302 for "constant known to fit". */
8304 /* Check if c >= type_low_bound. */
8305 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8307 dd = tree_to_double_int (type_low_bound);
8308 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8310 int c_neg = (!unsc && dc.is_negative ());
8311 int t_neg = (unsc && dd.is_negative ());
8313 if (c_neg && !t_neg)
8314 return false;
8315 if ((c_neg || !t_neg) && dc.ult (dd))
8316 return false;
8318 else if (dc.cmp (dd, unsc) < 0)
8319 return false;
8320 ok_for_low_bound = true;
8322 else
8323 ok_for_low_bound = false;
8325 /* Check if c <= type_high_bound. */
8326 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8328 dd = tree_to_double_int (type_high_bound);
8329 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8331 int c_neg = (!unsc && dc.is_negative ());
8332 int t_neg = (unsc && dd.is_negative ());
8334 if (t_neg && !c_neg)
8335 return false;
8336 if ((t_neg || !c_neg) && dc.ugt (dd))
8337 return false;
8339 else if (dc.cmp (dd, unsc) > 0)
8340 return false;
8341 ok_for_high_bound = true;
8343 else
8344 ok_for_high_bound = false;
8346 /* If the constant fits both bounds, the result is known. */
8347 if (ok_for_low_bound && ok_for_high_bound)
8348 return true;
8350 /* Perform some generic filtering which may allow making a decision
8351 even if the bounds are not constant. First, negative integers
8352 never fit in unsigned types, */
8353 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8354 return false;
8356 /* Second, narrower types always fit in wider ones. */
8357 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8358 return true;
8360 /* Third, unsigned integers with top bit set never fit signed types. */
8361 if (! TYPE_UNSIGNED (type) && unsc)
8363 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8364 if (prec < HOST_BITS_PER_WIDE_INT)
8366 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8367 return false;
8369 else if (((((unsigned HOST_WIDE_INT) 1)
8370 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8371 return false;
8374 /* If we haven't been able to decide at this point, there nothing more we
8375 can check ourselves here. Look at the base type if we have one and it
8376 has the same precision. */
8377 if (TREE_CODE (type) == INTEGER_TYPE
8378 && TREE_TYPE (type) != 0
8379 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8381 type = TREE_TYPE (type);
8382 goto retry;
8385 /* Or to double_int_fits_to_tree_p, if nothing else. */
8386 return double_int_fits_to_tree_p (type, dc);
8389 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8390 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8391 represented (assuming two's-complement arithmetic) within the bit
8392 precision of the type are returned instead. */
8394 void
8395 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8397 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8398 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8399 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8400 TYPE_UNSIGNED (type));
8401 else
8403 if (TYPE_UNSIGNED (type))
8404 mpz_set_ui (min, 0);
8405 else
8407 double_int mn;
8408 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8409 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8410 mpz_set_double_int (min, mn, false);
8414 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8415 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8416 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8417 TYPE_UNSIGNED (type));
8418 else
8420 if (TYPE_UNSIGNED (type))
8421 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8422 true);
8423 else
8424 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8425 true);
8429 /* Return true if VAR is an automatic variable defined in function FN. */
8431 bool
8432 auto_var_in_fn_p (const_tree var, const_tree fn)
8434 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8435 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8436 || TREE_CODE (var) == PARM_DECL)
8437 && ! TREE_STATIC (var))
8438 || TREE_CODE (var) == LABEL_DECL
8439 || TREE_CODE (var) == RESULT_DECL));
8442 /* Subprogram of following function. Called by walk_tree.
8444 Return *TP if it is an automatic variable or parameter of the
8445 function passed in as DATA. */
8447 static tree
8448 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8450 tree fn = (tree) data;
8452 if (TYPE_P (*tp))
8453 *walk_subtrees = 0;
8455 else if (DECL_P (*tp)
8456 && auto_var_in_fn_p (*tp, fn))
8457 return *tp;
8459 return NULL_TREE;
8462 /* Returns true if T is, contains, or refers to a type with variable
8463 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8464 arguments, but not the return type. If FN is nonzero, only return
8465 true if a modifier of the type or position of FN is a variable or
8466 parameter inside FN.
8468 This concept is more general than that of C99 'variably modified types':
8469 in C99, a struct type is never variably modified because a VLA may not
8470 appear as a structure member. However, in GNU C code like:
8472 struct S { int i[f()]; };
8474 is valid, and other languages may define similar constructs. */
8476 bool
8477 variably_modified_type_p (tree type, tree fn)
8479 tree t;
8481 /* Test if T is either variable (if FN is zero) or an expression containing
8482 a variable in FN. If TYPE isn't gimplified, return true also if
8483 gimplify_one_sizepos would gimplify the expression into a local
8484 variable. */
8485 #define RETURN_TRUE_IF_VAR(T) \
8486 do { tree _t = (T); \
8487 if (_t != NULL_TREE \
8488 && _t != error_mark_node \
8489 && TREE_CODE (_t) != INTEGER_CST \
8490 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8491 && (!fn \
8492 || (!TYPE_SIZES_GIMPLIFIED (type) \
8493 && !is_gimple_sizepos (_t)) \
8494 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8495 return true; } while (0)
8497 if (type == error_mark_node)
8498 return false;
8500 /* If TYPE itself has variable size, it is variably modified. */
8501 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8502 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8504 switch (TREE_CODE (type))
8506 case POINTER_TYPE:
8507 case REFERENCE_TYPE:
8508 case VECTOR_TYPE:
8509 if (variably_modified_type_p (TREE_TYPE (type), fn))
8510 return true;
8511 break;
8513 case FUNCTION_TYPE:
8514 case METHOD_TYPE:
8515 /* If TYPE is a function type, it is variably modified if the
8516 return type is variably modified. */
8517 if (variably_modified_type_p (TREE_TYPE (type), fn))
8518 return true;
8519 break;
8521 case INTEGER_TYPE:
8522 case REAL_TYPE:
8523 case FIXED_POINT_TYPE:
8524 case ENUMERAL_TYPE:
8525 case BOOLEAN_TYPE:
8526 /* Scalar types are variably modified if their end points
8527 aren't constant. */
8528 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8529 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8530 break;
8532 case RECORD_TYPE:
8533 case UNION_TYPE:
8534 case QUAL_UNION_TYPE:
8535 /* We can't see if any of the fields are variably-modified by the
8536 definition we normally use, since that would produce infinite
8537 recursion via pointers. */
8538 /* This is variably modified if some field's type is. */
8539 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8540 if (TREE_CODE (t) == FIELD_DECL)
8542 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8543 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8544 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8546 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8547 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8549 break;
8551 case ARRAY_TYPE:
8552 /* Do not call ourselves to avoid infinite recursion. This is
8553 variably modified if the element type is. */
8554 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8555 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8556 break;
8558 default:
8559 break;
8562 /* The current language may have other cases to check, but in general,
8563 all other types are not variably modified. */
8564 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8566 #undef RETURN_TRUE_IF_VAR
8569 /* Given a DECL or TYPE, return the scope in which it was declared, or
8570 NULL_TREE if there is no containing scope. */
8572 tree
8573 get_containing_scope (const_tree t)
8575 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8578 /* Return the innermost context enclosing DECL that is
8579 a FUNCTION_DECL, or zero if none. */
8581 tree
8582 decl_function_context (const_tree decl)
8584 tree context;
8586 if (TREE_CODE (decl) == ERROR_MARK)
8587 return 0;
8589 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8590 where we look up the function at runtime. Such functions always take
8591 a first argument of type 'pointer to real context'.
8593 C++ should really be fixed to use DECL_CONTEXT for the real context,
8594 and use something else for the "virtual context". */
8595 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8596 context
8597 = TYPE_MAIN_VARIANT
8598 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8599 else
8600 context = DECL_CONTEXT (decl);
8602 while (context && TREE_CODE (context) != FUNCTION_DECL)
8604 if (TREE_CODE (context) == BLOCK)
8605 context = BLOCK_SUPERCONTEXT (context);
8606 else
8607 context = get_containing_scope (context);
8610 return context;
8613 /* Return the innermost context enclosing DECL that is
8614 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8615 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8617 tree
8618 decl_type_context (const_tree decl)
8620 tree context = DECL_CONTEXT (decl);
8622 while (context)
8623 switch (TREE_CODE (context))
8625 case NAMESPACE_DECL:
8626 case TRANSLATION_UNIT_DECL:
8627 return NULL_TREE;
8629 case RECORD_TYPE:
8630 case UNION_TYPE:
8631 case QUAL_UNION_TYPE:
8632 return context;
8634 case TYPE_DECL:
8635 case FUNCTION_DECL:
8636 context = DECL_CONTEXT (context);
8637 break;
8639 case BLOCK:
8640 context = BLOCK_SUPERCONTEXT (context);
8641 break;
8643 default:
8644 gcc_unreachable ();
8647 return NULL_TREE;
8650 /* CALL is a CALL_EXPR. Return the declaration for the function
8651 called, or NULL_TREE if the called function cannot be
8652 determined. */
8654 tree
8655 get_callee_fndecl (const_tree call)
8657 tree addr;
8659 if (call == error_mark_node)
8660 return error_mark_node;
8662 /* It's invalid to call this function with anything but a
8663 CALL_EXPR. */
8664 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8666 /* The first operand to the CALL is the address of the function
8667 called. */
8668 addr = CALL_EXPR_FN (call);
8670 STRIP_NOPS (addr);
8672 /* If this is a readonly function pointer, extract its initial value. */
8673 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8674 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8675 && DECL_INITIAL (addr))
8676 addr = DECL_INITIAL (addr);
8678 /* If the address is just `&f' for some function `f', then we know
8679 that `f' is being called. */
8680 if (TREE_CODE (addr) == ADDR_EXPR
8681 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8682 return TREE_OPERAND (addr, 0);
8684 /* We couldn't figure out what was being called. */
8685 return NULL_TREE;
8688 /* Print debugging information about tree nodes generated during the compile,
8689 and any language-specific information. */
8691 void
8692 dump_tree_statistics (void)
8694 if (GATHER_STATISTICS)
8696 int i;
8697 int total_nodes, total_bytes;
8698 fprintf (stderr, "Kind Nodes Bytes\n");
8699 fprintf (stderr, "---------------------------------------\n");
8700 total_nodes = total_bytes = 0;
8701 for (i = 0; i < (int) all_kinds; i++)
8703 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8704 tree_node_counts[i], tree_node_sizes[i]);
8705 total_nodes += tree_node_counts[i];
8706 total_bytes += tree_node_sizes[i];
8708 fprintf (stderr, "---------------------------------------\n");
8709 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8710 fprintf (stderr, "---------------------------------------\n");
8711 fprintf (stderr, "Code Nodes\n");
8712 fprintf (stderr, "----------------------------\n");
8713 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8714 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8715 fprintf (stderr, "----------------------------\n");
8716 ssanames_print_statistics ();
8717 phinodes_print_statistics ();
8719 else
8720 fprintf (stderr, "(No per-node statistics)\n");
8722 print_type_hash_statistics ();
8723 print_debug_expr_statistics ();
8724 print_value_expr_statistics ();
8725 lang_hooks.print_statistics ();
8728 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8730 /* Generate a crc32 of a byte. */
8732 static unsigned
8733 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8735 unsigned ix;
8737 for (ix = bits; ix--; value <<= 1)
8739 unsigned feedback;
8741 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8742 chksum <<= 1;
8743 chksum ^= feedback;
8745 return chksum;
8748 /* Generate a crc32 of a 32-bit unsigned. */
8750 unsigned
8751 crc32_unsigned (unsigned chksum, unsigned value)
8753 return crc32_unsigned_bits (chksum, value, 32);
8756 /* Generate a crc32 of a byte. */
8758 unsigned
8759 crc32_byte (unsigned chksum, char byte)
8761 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8764 /* Generate a crc32 of a string. */
8766 unsigned
8767 crc32_string (unsigned chksum, const char *string)
8771 chksum = crc32_byte (chksum, *string);
8773 while (*string++);
8774 return chksum;
8777 /* P is a string that will be used in a symbol. Mask out any characters
8778 that are not valid in that context. */
8780 void
8781 clean_symbol_name (char *p)
8783 for (; *p; p++)
8784 if (! (ISALNUM (*p)
8785 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8786 || *p == '$'
8787 #endif
8788 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8789 || *p == '.'
8790 #endif
8792 *p = '_';
8795 /* Generate a name for a special-purpose function.
8796 The generated name may need to be unique across the whole link.
8797 Changes to this function may also require corresponding changes to
8798 xstrdup_mask_random.
8799 TYPE is some string to identify the purpose of this function to the
8800 linker or collect2; it must start with an uppercase letter,
8801 one of:
8802 I - for constructors
8803 D - for destructors
8804 N - for C++ anonymous namespaces
8805 F - for DWARF unwind frame information. */
8807 tree
8808 get_file_function_name (const char *type)
8810 char *buf;
8811 const char *p;
8812 char *q;
8814 /* If we already have a name we know to be unique, just use that. */
8815 if (first_global_object_name)
8816 p = q = ASTRDUP (first_global_object_name);
8817 /* If the target is handling the constructors/destructors, they
8818 will be local to this file and the name is only necessary for
8819 debugging purposes.
8820 We also assign sub_I and sub_D sufixes to constructors called from
8821 the global static constructors. These are always local. */
8822 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8823 || (strncmp (type, "sub_", 4) == 0
8824 && (type[4] == 'I' || type[4] == 'D')))
8826 const char *file = main_input_filename;
8827 if (! file)
8828 file = input_filename;
8829 /* Just use the file's basename, because the full pathname
8830 might be quite long. */
8831 p = q = ASTRDUP (lbasename (file));
8833 else
8835 /* Otherwise, the name must be unique across the entire link.
8836 We don't have anything that we know to be unique to this translation
8837 unit, so use what we do have and throw in some randomness. */
8838 unsigned len;
8839 const char *name = weak_global_object_name;
8840 const char *file = main_input_filename;
8842 if (! name)
8843 name = "";
8844 if (! file)
8845 file = input_filename;
8847 len = strlen (file);
8848 q = (char *) alloca (9 + 17 + len + 1);
8849 memcpy (q, file, len + 1);
8851 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8852 crc32_string (0, name), get_random_seed (false));
8854 p = q;
8857 clean_symbol_name (q);
8858 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8859 + strlen (type));
8861 /* Set up the name of the file-level functions we may need.
8862 Use a global object (which is already required to be unique over
8863 the program) rather than the file name (which imposes extra
8864 constraints). */
8865 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8867 return get_identifier (buf);
8870 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8872 /* Complain that the tree code of NODE does not match the expected 0
8873 terminated list of trailing codes. The trailing code list can be
8874 empty, for a more vague error message. FILE, LINE, and FUNCTION
8875 are of the caller. */
8877 void
8878 tree_check_failed (const_tree node, const char *file,
8879 int line, const char *function, ...)
8881 va_list args;
8882 const char *buffer;
8883 unsigned length = 0;
8884 int code;
8886 va_start (args, function);
8887 while ((code = va_arg (args, int)))
8888 length += 4 + strlen (tree_code_name[code]);
8889 va_end (args);
8890 if (length)
8892 char *tmp;
8893 va_start (args, function);
8894 length += strlen ("expected ");
8895 buffer = tmp = (char *) alloca (length);
8896 length = 0;
8897 while ((code = va_arg (args, int)))
8899 const char *prefix = length ? " or " : "expected ";
8901 strcpy (tmp + length, prefix);
8902 length += strlen (prefix);
8903 strcpy (tmp + length, tree_code_name[code]);
8904 length += strlen (tree_code_name[code]);
8906 va_end (args);
8908 else
8909 buffer = "unexpected node";
8911 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8912 buffer, tree_code_name[TREE_CODE (node)],
8913 function, trim_filename (file), line);
8916 /* Complain that the tree code of NODE does match the expected 0
8917 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8918 the caller. */
8920 void
8921 tree_not_check_failed (const_tree node, const char *file,
8922 int line, const char *function, ...)
8924 va_list args;
8925 char *buffer;
8926 unsigned length = 0;
8927 int code;
8929 va_start (args, function);
8930 while ((code = va_arg (args, int)))
8931 length += 4 + strlen (tree_code_name[code]);
8932 va_end (args);
8933 va_start (args, function);
8934 buffer = (char *) alloca (length);
8935 length = 0;
8936 while ((code = va_arg (args, int)))
8938 if (length)
8940 strcpy (buffer + length, " or ");
8941 length += 4;
8943 strcpy (buffer + length, tree_code_name[code]);
8944 length += strlen (tree_code_name[code]);
8946 va_end (args);
8948 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8949 buffer, tree_code_name[TREE_CODE (node)],
8950 function, trim_filename (file), line);
8953 /* Similar to tree_check_failed, except that we check for a class of tree
8954 code, given in CL. */
8956 void
8957 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8958 const char *file, int line, const char *function)
8960 internal_error
8961 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8962 TREE_CODE_CLASS_STRING (cl),
8963 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8964 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8967 /* Similar to tree_check_failed, except that instead of specifying a
8968 dozen codes, use the knowledge that they're all sequential. */
8970 void
8971 tree_range_check_failed (const_tree node, const char *file, int line,
8972 const char *function, enum tree_code c1,
8973 enum tree_code c2)
8975 char *buffer;
8976 unsigned length = 0;
8977 unsigned int c;
8979 for (c = c1; c <= c2; ++c)
8980 length += 4 + strlen (tree_code_name[c]);
8982 length += strlen ("expected ");
8983 buffer = (char *) alloca (length);
8984 length = 0;
8986 for (c = c1; c <= c2; ++c)
8988 const char *prefix = length ? " or " : "expected ";
8990 strcpy (buffer + length, prefix);
8991 length += strlen (prefix);
8992 strcpy (buffer + length, tree_code_name[c]);
8993 length += strlen (tree_code_name[c]);
8996 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8997 buffer, tree_code_name[TREE_CODE (node)],
8998 function, trim_filename (file), line);
9002 /* Similar to tree_check_failed, except that we check that a tree does
9003 not have the specified code, given in CL. */
9005 void
9006 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9007 const char *file, int line, const char *function)
9009 internal_error
9010 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9011 TREE_CODE_CLASS_STRING (cl),
9012 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9013 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9017 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9019 void
9020 omp_clause_check_failed (const_tree node, const char *file, int line,
9021 const char *function, enum omp_clause_code code)
9023 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9024 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9025 function, trim_filename (file), line);
9029 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9031 void
9032 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9033 const char *function, enum omp_clause_code c1,
9034 enum omp_clause_code c2)
9036 char *buffer;
9037 unsigned length = 0;
9038 unsigned int c;
9040 for (c = c1; c <= c2; ++c)
9041 length += 4 + strlen (omp_clause_code_name[c]);
9043 length += strlen ("expected ");
9044 buffer = (char *) alloca (length);
9045 length = 0;
9047 for (c = c1; c <= c2; ++c)
9049 const char *prefix = length ? " or " : "expected ";
9051 strcpy (buffer + length, prefix);
9052 length += strlen (prefix);
9053 strcpy (buffer + length, omp_clause_code_name[c]);
9054 length += strlen (omp_clause_code_name[c]);
9057 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9058 buffer, omp_clause_code_name[TREE_CODE (node)],
9059 function, trim_filename (file), line);
9063 #undef DEFTREESTRUCT
9064 #define DEFTREESTRUCT(VAL, NAME) NAME,
9066 static const char *ts_enum_names[] = {
9067 #include "treestruct.def"
9069 #undef DEFTREESTRUCT
9071 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9073 /* Similar to tree_class_check_failed, except that we check for
9074 whether CODE contains the tree structure identified by EN. */
9076 void
9077 tree_contains_struct_check_failed (const_tree node,
9078 const enum tree_node_structure_enum en,
9079 const char *file, int line,
9080 const char *function)
9082 internal_error
9083 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9084 TS_ENUM_NAME(en),
9085 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9089 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9090 (dynamically sized) vector. */
9092 void
9093 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9094 const char *function)
9096 internal_error
9097 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9098 idx + 1, len, function, trim_filename (file), line);
9101 /* Similar to above, except that the check is for the bounds of the operand
9102 vector of an expression node EXP. */
9104 void
9105 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9106 int line, const char *function)
9108 int code = TREE_CODE (exp);
9109 internal_error
9110 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9111 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9112 function, trim_filename (file), line);
9115 /* Similar to above, except that the check is for the number of
9116 operands of an OMP_CLAUSE node. */
9118 void
9119 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9120 int line, const char *function)
9122 internal_error
9123 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9124 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9125 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9126 trim_filename (file), line);
9128 #endif /* ENABLE_TREE_CHECKING */
9130 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9131 and mapped to the machine mode MODE. Initialize its fields and build
9132 the information necessary for debugging output. */
9134 static tree
9135 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9137 tree t;
9138 hashval_t hashcode = 0;
9140 t = make_node (VECTOR_TYPE);
9141 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9142 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9143 SET_TYPE_MODE (t, mode);
9145 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9146 SET_TYPE_STRUCTURAL_EQUALITY (t);
9147 else if (TYPE_CANONICAL (innertype) != innertype
9148 || mode != VOIDmode)
9149 TYPE_CANONICAL (t)
9150 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9152 layout_type (t);
9154 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9155 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9156 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9157 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9158 t = type_hash_canon (hashcode, t);
9160 /* We have built a main variant, based on the main variant of the
9161 inner type. Use it to build the variant we return. */
9162 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9163 && TREE_TYPE (t) != innertype)
9164 return build_type_attribute_qual_variant (t,
9165 TYPE_ATTRIBUTES (innertype),
9166 TYPE_QUALS (innertype));
9168 return t;
9171 static tree
9172 make_or_reuse_type (unsigned size, int unsignedp)
9174 if (size == INT_TYPE_SIZE)
9175 return unsignedp ? unsigned_type_node : integer_type_node;
9176 if (size == CHAR_TYPE_SIZE)
9177 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9178 if (size == SHORT_TYPE_SIZE)
9179 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9180 if (size == LONG_TYPE_SIZE)
9181 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9182 if (size == LONG_LONG_TYPE_SIZE)
9183 return (unsignedp ? long_long_unsigned_type_node
9184 : long_long_integer_type_node);
9185 if (size == 128 && int128_integer_type_node)
9186 return (unsignedp ? int128_unsigned_type_node
9187 : int128_integer_type_node);
9189 if (unsignedp)
9190 return make_unsigned_type (size);
9191 else
9192 return make_signed_type (size);
9195 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9197 static tree
9198 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9200 if (satp)
9202 if (size == SHORT_FRACT_TYPE_SIZE)
9203 return unsignedp ? sat_unsigned_short_fract_type_node
9204 : sat_short_fract_type_node;
9205 if (size == FRACT_TYPE_SIZE)
9206 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9207 if (size == LONG_FRACT_TYPE_SIZE)
9208 return unsignedp ? sat_unsigned_long_fract_type_node
9209 : sat_long_fract_type_node;
9210 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9211 return unsignedp ? sat_unsigned_long_long_fract_type_node
9212 : sat_long_long_fract_type_node;
9214 else
9216 if (size == SHORT_FRACT_TYPE_SIZE)
9217 return unsignedp ? unsigned_short_fract_type_node
9218 : short_fract_type_node;
9219 if (size == FRACT_TYPE_SIZE)
9220 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9221 if (size == LONG_FRACT_TYPE_SIZE)
9222 return unsignedp ? unsigned_long_fract_type_node
9223 : long_fract_type_node;
9224 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9225 return unsignedp ? unsigned_long_long_fract_type_node
9226 : long_long_fract_type_node;
9229 return make_fract_type (size, unsignedp, satp);
9232 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9234 static tree
9235 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9237 if (satp)
9239 if (size == SHORT_ACCUM_TYPE_SIZE)
9240 return unsignedp ? sat_unsigned_short_accum_type_node
9241 : sat_short_accum_type_node;
9242 if (size == ACCUM_TYPE_SIZE)
9243 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9244 if (size == LONG_ACCUM_TYPE_SIZE)
9245 return unsignedp ? sat_unsigned_long_accum_type_node
9246 : sat_long_accum_type_node;
9247 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9248 return unsignedp ? sat_unsigned_long_long_accum_type_node
9249 : sat_long_long_accum_type_node;
9251 else
9253 if (size == SHORT_ACCUM_TYPE_SIZE)
9254 return unsignedp ? unsigned_short_accum_type_node
9255 : short_accum_type_node;
9256 if (size == ACCUM_TYPE_SIZE)
9257 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9258 if (size == LONG_ACCUM_TYPE_SIZE)
9259 return unsignedp ? unsigned_long_accum_type_node
9260 : long_accum_type_node;
9261 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9262 return unsignedp ? unsigned_long_long_accum_type_node
9263 : long_long_accum_type_node;
9266 return make_accum_type (size, unsignedp, satp);
9269 /* Create nodes for all integer types (and error_mark_node) using the sizes
9270 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9271 SHORT_DOUBLE specifies whether double should be of the same precision
9272 as float. */
9274 void
9275 build_common_tree_nodes (bool signed_char, bool short_double)
9277 error_mark_node = make_node (ERROR_MARK);
9278 TREE_TYPE (error_mark_node) = error_mark_node;
9280 initialize_sizetypes ();
9282 /* Define both `signed char' and `unsigned char'. */
9283 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9284 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9285 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9286 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9288 /* Define `char', which is like either `signed char' or `unsigned char'
9289 but not the same as either. */
9290 char_type_node
9291 = (signed_char
9292 ? make_signed_type (CHAR_TYPE_SIZE)
9293 : make_unsigned_type (CHAR_TYPE_SIZE));
9294 TYPE_STRING_FLAG (char_type_node) = 1;
9296 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9297 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9298 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9299 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9300 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9301 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9302 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9303 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9304 #if HOST_BITS_PER_WIDE_INT >= 64
9305 /* TODO: This isn't correct, but as logic depends at the moment on
9306 host's instead of target's wide-integer.
9307 If there is a target not supporting TImode, but has an 128-bit
9308 integer-scalar register, this target check needs to be adjusted. */
9309 if (targetm.scalar_mode_supported_p (TImode))
9311 int128_integer_type_node = make_signed_type (128);
9312 int128_unsigned_type_node = make_unsigned_type (128);
9314 #endif
9316 /* Define a boolean type. This type only represents boolean values but
9317 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9318 Front ends which want to override this size (i.e. Java) can redefine
9319 boolean_type_node before calling build_common_tree_nodes_2. */
9320 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9321 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9322 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9323 TYPE_PRECISION (boolean_type_node) = 1;
9325 /* Define what type to use for size_t. */
9326 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9327 size_type_node = unsigned_type_node;
9328 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9329 size_type_node = long_unsigned_type_node;
9330 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9331 size_type_node = long_long_unsigned_type_node;
9332 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9333 size_type_node = short_unsigned_type_node;
9334 else
9335 gcc_unreachable ();
9337 /* Fill in the rest of the sized types. Reuse existing type nodes
9338 when possible. */
9339 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9340 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9341 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9342 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9343 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9345 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9346 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9347 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9348 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9349 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9351 access_public_node = get_identifier ("public");
9352 access_protected_node = get_identifier ("protected");
9353 access_private_node = get_identifier ("private");
9355 /* Define these next since types below may used them. */
9356 integer_zero_node = build_int_cst (integer_type_node, 0);
9357 integer_one_node = build_int_cst (integer_type_node, 1);
9358 integer_three_node = build_int_cst (integer_type_node, 3);
9359 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9361 size_zero_node = size_int (0);
9362 size_one_node = size_int (1);
9363 bitsize_zero_node = bitsize_int (0);
9364 bitsize_one_node = bitsize_int (1);
9365 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9367 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9368 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9370 void_type_node = make_node (VOID_TYPE);
9371 layout_type (void_type_node);
9373 /* We are not going to have real types in C with less than byte alignment,
9374 so we might as well not have any types that claim to have it. */
9375 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9376 TYPE_USER_ALIGN (void_type_node) = 0;
9378 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9379 layout_type (TREE_TYPE (null_pointer_node));
9381 ptr_type_node = build_pointer_type (void_type_node);
9382 const_ptr_type_node
9383 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9384 fileptr_type_node = ptr_type_node;
9386 float_type_node = make_node (REAL_TYPE);
9387 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9388 layout_type (float_type_node);
9390 double_type_node = make_node (REAL_TYPE);
9391 if (short_double)
9392 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9393 else
9394 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9395 layout_type (double_type_node);
9397 long_double_type_node = make_node (REAL_TYPE);
9398 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9399 layout_type (long_double_type_node);
9401 float_ptr_type_node = build_pointer_type (float_type_node);
9402 double_ptr_type_node = build_pointer_type (double_type_node);
9403 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9404 integer_ptr_type_node = build_pointer_type (integer_type_node);
9406 /* Fixed size integer types. */
9407 uint16_type_node = build_nonstandard_integer_type (16, true);
9408 uint32_type_node = build_nonstandard_integer_type (32, true);
9409 uint64_type_node = build_nonstandard_integer_type (64, true);
9411 /* Decimal float types. */
9412 dfloat32_type_node = make_node (REAL_TYPE);
9413 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9414 layout_type (dfloat32_type_node);
9415 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9416 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9418 dfloat64_type_node = make_node (REAL_TYPE);
9419 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9420 layout_type (dfloat64_type_node);
9421 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9422 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9424 dfloat128_type_node = make_node (REAL_TYPE);
9425 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9426 layout_type (dfloat128_type_node);
9427 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9428 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9430 complex_integer_type_node = build_complex_type (integer_type_node);
9431 complex_float_type_node = build_complex_type (float_type_node);
9432 complex_double_type_node = build_complex_type (double_type_node);
9433 complex_long_double_type_node = build_complex_type (long_double_type_node);
9435 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9436 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9437 sat_ ## KIND ## _type_node = \
9438 make_sat_signed_ ## KIND ## _type (SIZE); \
9439 sat_unsigned_ ## KIND ## _type_node = \
9440 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9441 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9442 unsigned_ ## KIND ## _type_node = \
9443 make_unsigned_ ## KIND ## _type (SIZE);
9445 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9446 sat_ ## WIDTH ## KIND ## _type_node = \
9447 make_sat_signed_ ## KIND ## _type (SIZE); \
9448 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9449 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9450 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9451 unsigned_ ## WIDTH ## KIND ## _type_node = \
9452 make_unsigned_ ## KIND ## _type (SIZE);
9454 /* Make fixed-point type nodes based on four different widths. */
9455 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9456 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9457 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9458 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9459 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9461 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9462 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9463 NAME ## _type_node = \
9464 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9465 u ## NAME ## _type_node = \
9466 make_or_reuse_unsigned_ ## KIND ## _type \
9467 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9468 sat_ ## NAME ## _type_node = \
9469 make_or_reuse_sat_signed_ ## KIND ## _type \
9470 (GET_MODE_BITSIZE (MODE ## mode)); \
9471 sat_u ## NAME ## _type_node = \
9472 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9473 (GET_MODE_BITSIZE (U ## MODE ## mode));
9475 /* Fixed-point type and mode nodes. */
9476 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9477 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9478 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9479 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9480 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9481 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9482 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9483 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9484 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9485 MAKE_FIXED_MODE_NODE (accum, da, DA)
9486 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9489 tree t = targetm.build_builtin_va_list ();
9491 /* Many back-ends define record types without setting TYPE_NAME.
9492 If we copied the record type here, we'd keep the original
9493 record type without a name. This breaks name mangling. So,
9494 don't copy record types and let c_common_nodes_and_builtins()
9495 declare the type to be __builtin_va_list. */
9496 if (TREE_CODE (t) != RECORD_TYPE)
9497 t = build_variant_type_copy (t);
9499 va_list_type_node = t;
9503 /* Modify DECL for given flags. */
9504 void
9505 set_call_expr_flags (tree decl, int flags)
9507 if (flags & ECF_NOTHROW)
9508 TREE_NOTHROW (decl) = 1;
9509 if (flags & ECF_CONST)
9510 TREE_READONLY (decl) = 1;
9511 if (flags & ECF_PURE)
9512 DECL_PURE_P (decl) = 1;
9513 if (flags & ECF_LOOPING_CONST_OR_PURE)
9514 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9515 if (flags & ECF_NOVOPS)
9516 DECL_IS_NOVOPS (decl) = 1;
9517 if (flags & ECF_NORETURN)
9518 TREE_THIS_VOLATILE (decl) = 1;
9519 if (flags & ECF_MALLOC)
9520 DECL_IS_MALLOC (decl) = 1;
9521 if (flags & ECF_RETURNS_TWICE)
9522 DECL_IS_RETURNS_TWICE (decl) = 1;
9523 if (flags & ECF_LEAF)
9524 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9525 NULL, DECL_ATTRIBUTES (decl));
9526 if ((flags & ECF_TM_PURE) && flag_tm)
9527 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9528 NULL, DECL_ATTRIBUTES (decl));
9529 /* Looping const or pure is implied by noreturn.
9530 There is currently no way to declare looping const or looping pure alone. */
9531 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9532 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9536 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9538 static void
9539 local_define_builtin (const char *name, tree type, enum built_in_function code,
9540 const char *library_name, int ecf_flags)
9542 tree decl;
9544 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9545 library_name, NULL_TREE);
9546 set_call_expr_flags (decl, ecf_flags);
9548 set_builtin_decl (code, decl, true);
9551 /* Call this function after instantiating all builtins that the language
9552 front end cares about. This will build the rest of the builtins that
9553 are relied upon by the tree optimizers and the middle-end. */
9555 void
9556 build_common_builtin_nodes (void)
9558 tree tmp, ftype;
9559 int ecf_flags;
9561 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9563 ftype = build_function_type (void_type_node, void_list_node);
9564 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9565 "__builtin_unreachable",
9566 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9567 | ECF_CONST | ECF_LEAF);
9570 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9571 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9573 ftype = build_function_type_list (ptr_type_node,
9574 ptr_type_node, const_ptr_type_node,
9575 size_type_node, NULL_TREE);
9577 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9578 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9579 "memcpy", ECF_NOTHROW | ECF_LEAF);
9580 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9581 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9582 "memmove", ECF_NOTHROW | ECF_LEAF);
9585 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9587 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9588 const_ptr_type_node, size_type_node,
9589 NULL_TREE);
9590 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9591 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9594 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9596 ftype = build_function_type_list (ptr_type_node,
9597 ptr_type_node, integer_type_node,
9598 size_type_node, NULL_TREE);
9599 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9600 "memset", ECF_NOTHROW | ECF_LEAF);
9603 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9605 ftype = build_function_type_list (ptr_type_node,
9606 size_type_node, NULL_TREE);
9607 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9608 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9611 ftype = build_function_type_list (ptr_type_node, size_type_node,
9612 size_type_node, NULL_TREE);
9613 local_define_builtin ("__builtin_alloca_with_align", ftype,
9614 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9615 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9617 /* If we're checking the stack, `alloca' can throw. */
9618 if (flag_stack_check)
9620 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9621 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9624 ftype = build_function_type_list (void_type_node,
9625 ptr_type_node, ptr_type_node,
9626 ptr_type_node, NULL_TREE);
9627 local_define_builtin ("__builtin_init_trampoline", ftype,
9628 BUILT_IN_INIT_TRAMPOLINE,
9629 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9630 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9631 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9632 "__builtin_init_heap_trampoline",
9633 ECF_NOTHROW | ECF_LEAF);
9635 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9636 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9637 BUILT_IN_ADJUST_TRAMPOLINE,
9638 "__builtin_adjust_trampoline",
9639 ECF_CONST | ECF_NOTHROW);
9641 ftype = build_function_type_list (void_type_node,
9642 ptr_type_node, ptr_type_node, NULL_TREE);
9643 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9644 BUILT_IN_NONLOCAL_GOTO,
9645 "__builtin_nonlocal_goto",
9646 ECF_NORETURN | ECF_NOTHROW);
9648 ftype = build_function_type_list (void_type_node,
9649 ptr_type_node, ptr_type_node, NULL_TREE);
9650 local_define_builtin ("__builtin_setjmp_setup", ftype,
9651 BUILT_IN_SETJMP_SETUP,
9652 "__builtin_setjmp_setup", ECF_NOTHROW);
9654 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9655 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9656 BUILT_IN_SETJMP_DISPATCHER,
9657 "__builtin_setjmp_dispatcher",
9658 ECF_PURE | ECF_NOTHROW);
9660 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9661 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9662 BUILT_IN_SETJMP_RECEIVER,
9663 "__builtin_setjmp_receiver", ECF_NOTHROW);
9665 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9666 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9667 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9669 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9670 local_define_builtin ("__builtin_stack_restore", ftype,
9671 BUILT_IN_STACK_RESTORE,
9672 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9674 /* If there's a possibility that we might use the ARM EABI, build the
9675 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9676 if (targetm.arm_eabi_unwinder)
9678 ftype = build_function_type_list (void_type_node, NULL_TREE);
9679 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9680 BUILT_IN_CXA_END_CLEANUP,
9681 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9684 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9685 local_define_builtin ("__builtin_unwind_resume", ftype,
9686 BUILT_IN_UNWIND_RESUME,
9687 ((targetm_common.except_unwind_info (&global_options)
9688 == UI_SJLJ)
9689 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9690 ECF_NORETURN);
9692 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9694 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9695 NULL_TREE);
9696 local_define_builtin ("__builtin_return_address", ftype,
9697 BUILT_IN_RETURN_ADDRESS,
9698 "__builtin_return_address",
9699 ECF_NOTHROW);
9702 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9703 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9705 ftype = build_function_type_list (void_type_node, ptr_type_node,
9706 ptr_type_node, NULL_TREE);
9707 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9708 local_define_builtin ("__cyg_profile_func_enter", ftype,
9709 BUILT_IN_PROFILE_FUNC_ENTER,
9710 "__cyg_profile_func_enter", 0);
9711 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9712 local_define_builtin ("__cyg_profile_func_exit", ftype,
9713 BUILT_IN_PROFILE_FUNC_EXIT,
9714 "__cyg_profile_func_exit", 0);
9717 /* The exception object and filter values from the runtime. The argument
9718 must be zero before exception lowering, i.e. from the front end. After
9719 exception lowering, it will be the region number for the exception
9720 landing pad. These functions are PURE instead of CONST to prevent
9721 them from being hoisted past the exception edge that will initialize
9722 its value in the landing pad. */
9723 ftype = build_function_type_list (ptr_type_node,
9724 integer_type_node, NULL_TREE);
9725 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9726 /* Only use TM_PURE if we we have TM language support. */
9727 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9728 ecf_flags |= ECF_TM_PURE;
9729 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9730 "__builtin_eh_pointer", ecf_flags);
9732 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9733 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9734 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9735 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9737 ftype = build_function_type_list (void_type_node,
9738 integer_type_node, integer_type_node,
9739 NULL_TREE);
9740 local_define_builtin ("__builtin_eh_copy_values", ftype,
9741 BUILT_IN_EH_COPY_VALUES,
9742 "__builtin_eh_copy_values", ECF_NOTHROW);
9744 /* Complex multiplication and division. These are handled as builtins
9745 rather than optabs because emit_library_call_value doesn't support
9746 complex. Further, we can do slightly better with folding these
9747 beasties if the real and complex parts of the arguments are separate. */
9749 int mode;
9751 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9753 char mode_name_buf[4], *q;
9754 const char *p;
9755 enum built_in_function mcode, dcode;
9756 tree type, inner_type;
9757 const char *prefix = "__";
9759 if (targetm.libfunc_gnu_prefix)
9760 prefix = "__gnu_";
9762 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9763 if (type == NULL)
9764 continue;
9765 inner_type = TREE_TYPE (type);
9767 ftype = build_function_type_list (type, inner_type, inner_type,
9768 inner_type, inner_type, NULL_TREE);
9770 mcode = ((enum built_in_function)
9771 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9772 dcode = ((enum built_in_function)
9773 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9775 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9776 *q = TOLOWER (*p);
9777 *q = '\0';
9779 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9780 NULL);
9781 local_define_builtin (built_in_names[mcode], ftype, mcode,
9782 built_in_names[mcode],
9783 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9785 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9786 NULL);
9787 local_define_builtin (built_in_names[dcode], ftype, dcode,
9788 built_in_names[dcode],
9789 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9794 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9795 better way.
9797 If we requested a pointer to a vector, build up the pointers that
9798 we stripped off while looking for the inner type. Similarly for
9799 return values from functions.
9801 The argument TYPE is the top of the chain, and BOTTOM is the
9802 new type which we will point to. */
9804 tree
9805 reconstruct_complex_type (tree type, tree bottom)
9807 tree inner, outer;
9809 if (TREE_CODE (type) == POINTER_TYPE)
9811 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9812 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9813 TYPE_REF_CAN_ALIAS_ALL (type));
9815 else if (TREE_CODE (type) == REFERENCE_TYPE)
9817 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9818 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9819 TYPE_REF_CAN_ALIAS_ALL (type));
9821 else if (TREE_CODE (type) == ARRAY_TYPE)
9823 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9824 outer = build_array_type (inner, TYPE_DOMAIN (type));
9826 else if (TREE_CODE (type) == FUNCTION_TYPE)
9828 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9829 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9831 else if (TREE_CODE (type) == METHOD_TYPE)
9833 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9834 /* The build_method_type_directly() routine prepends 'this' to argument list,
9835 so we must compensate by getting rid of it. */
9836 outer
9837 = build_method_type_directly
9838 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9839 inner,
9840 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9842 else if (TREE_CODE (type) == OFFSET_TYPE)
9844 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9845 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9847 else
9848 return bottom;
9850 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9851 TYPE_QUALS (type));
9854 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9855 the inner type. */
9856 tree
9857 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9859 int nunits;
9861 switch (GET_MODE_CLASS (mode))
9863 case MODE_VECTOR_INT:
9864 case MODE_VECTOR_FLOAT:
9865 case MODE_VECTOR_FRACT:
9866 case MODE_VECTOR_UFRACT:
9867 case MODE_VECTOR_ACCUM:
9868 case MODE_VECTOR_UACCUM:
9869 nunits = GET_MODE_NUNITS (mode);
9870 break;
9872 case MODE_INT:
9873 /* Check that there are no leftover bits. */
9874 gcc_assert (GET_MODE_BITSIZE (mode)
9875 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9877 nunits = GET_MODE_BITSIZE (mode)
9878 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9879 break;
9881 default:
9882 gcc_unreachable ();
9885 return make_vector_type (innertype, nunits, mode);
9888 /* Similarly, but takes the inner type and number of units, which must be
9889 a power of two. */
9891 tree
9892 build_vector_type (tree innertype, int nunits)
9894 return make_vector_type (innertype, nunits, VOIDmode);
9897 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9899 tree
9900 build_opaque_vector_type (tree innertype, int nunits)
9902 tree t = make_vector_type (innertype, nunits, VOIDmode);
9903 tree cand;
9904 /* We always build the non-opaque variant before the opaque one,
9905 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9906 cand = TYPE_NEXT_VARIANT (t);
9907 if (cand
9908 && TYPE_VECTOR_OPAQUE (cand)
9909 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9910 return cand;
9911 /* Othewise build a variant type and make sure to queue it after
9912 the non-opaque type. */
9913 cand = build_distinct_type_copy (t);
9914 TYPE_VECTOR_OPAQUE (cand) = true;
9915 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9916 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9917 TYPE_NEXT_VARIANT (t) = cand;
9918 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9919 return cand;
9923 /* Given an initializer INIT, return TRUE if INIT is zero or some
9924 aggregate of zeros. Otherwise return FALSE. */
9925 bool
9926 initializer_zerop (const_tree init)
9928 tree elt;
9930 STRIP_NOPS (init);
9932 switch (TREE_CODE (init))
9934 case INTEGER_CST:
9935 return integer_zerop (init);
9937 case REAL_CST:
9938 /* ??? Note that this is not correct for C4X float formats. There,
9939 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9940 negative exponent. */
9941 return real_zerop (init)
9942 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9944 case FIXED_CST:
9945 return fixed_zerop (init);
9947 case COMPLEX_CST:
9948 return integer_zerop (init)
9949 || (real_zerop (init)
9950 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9951 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9953 case VECTOR_CST:
9955 unsigned i;
9956 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
9957 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
9958 return false;
9959 return true;
9962 case CONSTRUCTOR:
9964 unsigned HOST_WIDE_INT idx;
9966 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
9967 if (!initializer_zerop (elt))
9968 return false;
9969 return true;
9972 case STRING_CST:
9974 int i;
9976 /* We need to loop through all elements to handle cases like
9977 "\0" and "\0foobar". */
9978 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
9979 if (TREE_STRING_POINTER (init)[i] != '\0')
9980 return false;
9982 return true;
9985 default:
9986 return false;
9990 /* Build an empty statement at location LOC. */
9992 tree
9993 build_empty_stmt (location_t loc)
9995 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
9996 SET_EXPR_LOCATION (t, loc);
9997 return t;
10001 /* Build an OpenMP clause with code CODE. LOC is the location of the
10002 clause. */
10004 tree
10005 build_omp_clause (location_t loc, enum omp_clause_code code)
10007 tree t;
10008 int size, length;
10010 length = omp_clause_num_ops[code];
10011 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10013 record_node_allocation_statistics (OMP_CLAUSE, size);
10015 t = ggc_alloc_tree_node (size);
10016 memset (t, 0, size);
10017 TREE_SET_CODE (t, OMP_CLAUSE);
10018 OMP_CLAUSE_SET_CODE (t, code);
10019 OMP_CLAUSE_LOCATION (t) = loc;
10021 return t;
10024 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10025 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10026 Except for the CODE and operand count field, other storage for the
10027 object is initialized to zeros. */
10029 tree
10030 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10032 tree t;
10033 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10035 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10036 gcc_assert (len >= 1);
10038 record_node_allocation_statistics (code, length);
10040 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10042 TREE_SET_CODE (t, code);
10044 /* Can't use TREE_OPERAND to store the length because if checking is
10045 enabled, it will try to check the length before we store it. :-P */
10046 t->exp.operands[0] = build_int_cst (sizetype, len);
10048 return t;
10051 /* Helper function for build_call_* functions; build a CALL_EXPR with
10052 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10053 the argument slots. */
10055 static tree
10056 build_call_1 (tree return_type, tree fn, int nargs)
10058 tree t;
10060 t = build_vl_exp (CALL_EXPR, nargs + 3);
10061 TREE_TYPE (t) = return_type;
10062 CALL_EXPR_FN (t) = fn;
10063 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10065 return t;
10068 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10069 FN and a null static chain slot. NARGS is the number of call arguments
10070 which are specified as "..." arguments. */
10072 tree
10073 build_call_nary (tree return_type, tree fn, int nargs, ...)
10075 tree ret;
10076 va_list args;
10077 va_start (args, nargs);
10078 ret = build_call_valist (return_type, fn, nargs, args);
10079 va_end (args);
10080 return ret;
10083 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10084 FN and a null static chain slot. NARGS is the number of call arguments
10085 which are specified as a va_list ARGS. */
10087 tree
10088 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10090 tree t;
10091 int i;
10093 t = build_call_1 (return_type, fn, nargs);
10094 for (i = 0; i < nargs; i++)
10095 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10096 process_call_operands (t);
10097 return t;
10100 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10101 FN and a null static chain slot. NARGS is the number of call arguments
10102 which are specified as a tree array ARGS. */
10104 tree
10105 build_call_array_loc (location_t loc, tree return_type, tree fn,
10106 int nargs, const tree *args)
10108 tree t;
10109 int i;
10111 t = build_call_1 (return_type, fn, nargs);
10112 for (i = 0; i < nargs; i++)
10113 CALL_EXPR_ARG (t, i) = args[i];
10114 process_call_operands (t);
10115 SET_EXPR_LOCATION (t, loc);
10116 return t;
10119 /* Like build_call_array, but takes a vec. */
10121 tree
10122 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10124 tree ret, t;
10125 unsigned int ix;
10127 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10128 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10129 CALL_EXPR_ARG (ret, ix) = t;
10130 process_call_operands (ret);
10131 return ret;
10135 /* Returns true if it is possible to prove that the index of
10136 an array access REF (an ARRAY_REF expression) falls into the
10137 array bounds. */
10139 bool
10140 in_array_bounds_p (tree ref)
10142 tree idx = TREE_OPERAND (ref, 1);
10143 tree min, max;
10145 if (TREE_CODE (idx) != INTEGER_CST)
10146 return false;
10148 min = array_ref_low_bound (ref);
10149 max = array_ref_up_bound (ref);
10150 if (!min
10151 || !max
10152 || TREE_CODE (min) != INTEGER_CST
10153 || TREE_CODE (max) != INTEGER_CST)
10154 return false;
10156 if (tree_int_cst_lt (idx, min)
10157 || tree_int_cst_lt (max, idx))
10158 return false;
10160 return true;
10163 /* Returns true if it is possible to prove that the range of
10164 an array access REF (an ARRAY_RANGE_REF expression) falls
10165 into the array bounds. */
10167 bool
10168 range_in_array_bounds_p (tree ref)
10170 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10171 tree range_min, range_max, min, max;
10173 range_min = TYPE_MIN_VALUE (domain_type);
10174 range_max = TYPE_MAX_VALUE (domain_type);
10175 if (!range_min
10176 || !range_max
10177 || TREE_CODE (range_min) != INTEGER_CST
10178 || TREE_CODE (range_max) != INTEGER_CST)
10179 return false;
10181 min = array_ref_low_bound (ref);
10182 max = array_ref_up_bound (ref);
10183 if (!min
10184 || !max
10185 || TREE_CODE (min) != INTEGER_CST
10186 || TREE_CODE (max) != INTEGER_CST)
10187 return false;
10189 if (tree_int_cst_lt (range_min, min)
10190 || tree_int_cst_lt (max, range_max))
10191 return false;
10193 return true;
10196 /* Return true if T (assumed to be a DECL) must be assigned a memory
10197 location. */
10199 bool
10200 needs_to_live_in_memory (const_tree t)
10202 return (TREE_ADDRESSABLE (t)
10203 || is_global_var (t)
10204 || (TREE_CODE (t) == RESULT_DECL
10205 && !DECL_BY_REFERENCE (t)
10206 && aggregate_value_p (t, current_function_decl)));
10209 /* Return value of a constant X and sign-extend it. */
10211 HOST_WIDE_INT
10212 int_cst_value (const_tree x)
10214 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10215 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10217 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10218 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10219 || TREE_INT_CST_HIGH (x) == -1);
10221 if (bits < HOST_BITS_PER_WIDE_INT)
10223 bool negative = ((val >> (bits - 1)) & 1) != 0;
10224 if (negative)
10225 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10226 else
10227 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10230 return val;
10233 /* Return value of a constant X and sign-extend it. */
10235 HOST_WIDEST_INT
10236 widest_int_cst_value (const_tree x)
10238 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10239 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10241 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10242 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10243 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10244 << HOST_BITS_PER_WIDE_INT);
10245 #else
10246 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10247 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10248 || TREE_INT_CST_HIGH (x) == -1);
10249 #endif
10251 if (bits < HOST_BITS_PER_WIDEST_INT)
10253 bool negative = ((val >> (bits - 1)) & 1) != 0;
10254 if (negative)
10255 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10256 else
10257 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10260 return val;
10263 /* If TYPE is an integral or pointer type, return an integer type with
10264 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10265 if TYPE is already an integer type of signedness UNSIGNEDP. */
10267 tree
10268 signed_or_unsigned_type_for (int unsignedp, tree type)
10270 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10271 return type;
10273 if (TREE_CODE (type) == VECTOR_TYPE)
10275 tree inner = TREE_TYPE (type);
10276 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10277 if (!inner2)
10278 return NULL_TREE;
10279 if (inner == inner2)
10280 return type;
10281 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10284 if (!INTEGRAL_TYPE_P (type)
10285 && !POINTER_TYPE_P (type))
10286 return NULL_TREE;
10288 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10291 /* If TYPE is an integral or pointer type, return an integer type with
10292 the same precision which is unsigned, or itself if TYPE is already an
10293 unsigned integer type. */
10295 tree
10296 unsigned_type_for (tree type)
10298 return signed_or_unsigned_type_for (1, type);
10301 /* If TYPE is an integral or pointer type, return an integer type with
10302 the same precision which is signed, or itself if TYPE is already a
10303 signed integer type. */
10305 tree
10306 signed_type_for (tree type)
10308 return signed_or_unsigned_type_for (0, type);
10311 /* If TYPE is a vector type, return a signed integer vector type with the
10312 same width and number of subparts. Otherwise return boolean_type_node. */
10314 tree
10315 truth_type_for (tree type)
10317 if (TREE_CODE (type) == VECTOR_TYPE)
10319 tree elem = lang_hooks.types.type_for_size
10320 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10321 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10323 else
10324 return boolean_type_node;
10327 /* Returns the largest value obtainable by casting something in INNER type to
10328 OUTER type. */
10330 tree
10331 upper_bound_in_type (tree outer, tree inner)
10333 double_int high;
10334 unsigned int det = 0;
10335 unsigned oprec = TYPE_PRECISION (outer);
10336 unsigned iprec = TYPE_PRECISION (inner);
10337 unsigned prec;
10339 /* Compute a unique number for every combination. */
10340 det |= (oprec > iprec) ? 4 : 0;
10341 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10342 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10344 /* Determine the exponent to use. */
10345 switch (det)
10347 case 0:
10348 case 1:
10349 /* oprec <= iprec, outer: signed, inner: don't care. */
10350 prec = oprec - 1;
10351 break;
10352 case 2:
10353 case 3:
10354 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10355 prec = oprec;
10356 break;
10357 case 4:
10358 /* oprec > iprec, outer: signed, inner: signed. */
10359 prec = iprec - 1;
10360 break;
10361 case 5:
10362 /* oprec > iprec, outer: signed, inner: unsigned. */
10363 prec = iprec;
10364 break;
10365 case 6:
10366 /* oprec > iprec, outer: unsigned, inner: signed. */
10367 prec = oprec;
10368 break;
10369 case 7:
10370 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10371 prec = iprec;
10372 break;
10373 default:
10374 gcc_unreachable ();
10377 /* Compute 2^^prec - 1. */
10378 if (prec <= HOST_BITS_PER_WIDE_INT)
10380 high.high = 0;
10381 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10382 >> (HOST_BITS_PER_WIDE_INT - prec));
10384 else
10386 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10387 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10388 high.low = ~(unsigned HOST_WIDE_INT) 0;
10391 return double_int_to_tree (outer, high);
10394 /* Returns the smallest value obtainable by casting something in INNER type to
10395 OUTER type. */
10397 tree
10398 lower_bound_in_type (tree outer, tree inner)
10400 double_int low;
10401 unsigned oprec = TYPE_PRECISION (outer);
10402 unsigned iprec = TYPE_PRECISION (inner);
10404 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10405 and obtain 0. */
10406 if (TYPE_UNSIGNED (outer)
10407 /* If we are widening something of an unsigned type, OUTER type
10408 contains all values of INNER type. In particular, both INNER
10409 and OUTER types have zero in common. */
10410 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10411 low.low = low.high = 0;
10412 else
10414 /* If we are widening a signed type to another signed type, we
10415 want to obtain -2^^(iprec-1). If we are keeping the
10416 precision or narrowing to a signed type, we want to obtain
10417 -2^(oprec-1). */
10418 unsigned prec = oprec > iprec ? iprec : oprec;
10420 if (prec <= HOST_BITS_PER_WIDE_INT)
10422 low.high = ~(unsigned HOST_WIDE_INT) 0;
10423 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10425 else
10427 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10428 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10429 low.low = 0;
10433 return double_int_to_tree (outer, low);
10436 /* Return nonzero if two operands that are suitable for PHI nodes are
10437 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10438 SSA_NAME or invariant. Note that this is strictly an optimization.
10439 That is, callers of this function can directly call operand_equal_p
10440 and get the same result, only slower. */
10443 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10445 if (arg0 == arg1)
10446 return 1;
10447 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10448 return 0;
10449 return operand_equal_p (arg0, arg1, 0);
10452 /* Returns number of zeros at the end of binary representation of X.
10454 ??? Use ffs if available? */
10456 tree
10457 num_ending_zeros (const_tree x)
10459 unsigned HOST_WIDE_INT fr, nfr;
10460 unsigned num, abits;
10461 tree type = TREE_TYPE (x);
10463 if (TREE_INT_CST_LOW (x) == 0)
10465 num = HOST_BITS_PER_WIDE_INT;
10466 fr = TREE_INT_CST_HIGH (x);
10468 else
10470 num = 0;
10471 fr = TREE_INT_CST_LOW (x);
10474 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10476 nfr = fr >> abits;
10477 if (nfr << abits == fr)
10479 num += abits;
10480 fr = nfr;
10484 if (num > TYPE_PRECISION (type))
10485 num = TYPE_PRECISION (type);
10487 return build_int_cst_type (type, num);
10491 #define WALK_SUBTREE(NODE) \
10492 do \
10494 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10495 if (result) \
10496 return result; \
10498 while (0)
10500 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10501 be walked whenever a type is seen in the tree. Rest of operands and return
10502 value are as for walk_tree. */
10504 static tree
10505 walk_type_fields (tree type, walk_tree_fn func, void *data,
10506 struct pointer_set_t *pset, walk_tree_lh lh)
10508 tree result = NULL_TREE;
10510 switch (TREE_CODE (type))
10512 case POINTER_TYPE:
10513 case REFERENCE_TYPE:
10514 /* We have to worry about mutually recursive pointers. These can't
10515 be written in C. They can in Ada. It's pathological, but
10516 there's an ACATS test (c38102a) that checks it. Deal with this
10517 by checking if we're pointing to another pointer, that one
10518 points to another pointer, that one does too, and we have no htab.
10519 If so, get a hash table. We check three levels deep to avoid
10520 the cost of the hash table if we don't need one. */
10521 if (POINTER_TYPE_P (TREE_TYPE (type))
10522 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10523 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10524 && !pset)
10526 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10527 func, data);
10528 if (result)
10529 return result;
10531 break;
10534 /* ... fall through ... */
10536 case COMPLEX_TYPE:
10537 WALK_SUBTREE (TREE_TYPE (type));
10538 break;
10540 case METHOD_TYPE:
10541 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10543 /* Fall through. */
10545 case FUNCTION_TYPE:
10546 WALK_SUBTREE (TREE_TYPE (type));
10548 tree arg;
10550 /* We never want to walk into default arguments. */
10551 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10552 WALK_SUBTREE (TREE_VALUE (arg));
10554 break;
10556 case ARRAY_TYPE:
10557 /* Don't follow this nodes's type if a pointer for fear that
10558 we'll have infinite recursion. If we have a PSET, then we
10559 need not fear. */
10560 if (pset
10561 || (!POINTER_TYPE_P (TREE_TYPE (type))
10562 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10563 WALK_SUBTREE (TREE_TYPE (type));
10564 WALK_SUBTREE (TYPE_DOMAIN (type));
10565 break;
10567 case OFFSET_TYPE:
10568 WALK_SUBTREE (TREE_TYPE (type));
10569 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10570 break;
10572 default:
10573 break;
10576 return NULL_TREE;
10579 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10580 called with the DATA and the address of each sub-tree. If FUNC returns a
10581 non-NULL value, the traversal is stopped, and the value returned by FUNC
10582 is returned. If PSET is non-NULL it is used to record the nodes visited,
10583 and to avoid visiting a node more than once. */
10585 tree
10586 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10587 struct pointer_set_t *pset, walk_tree_lh lh)
10589 enum tree_code code;
10590 int walk_subtrees;
10591 tree result;
10593 #define WALK_SUBTREE_TAIL(NODE) \
10594 do \
10596 tp = & (NODE); \
10597 goto tail_recurse; \
10599 while (0)
10601 tail_recurse:
10602 /* Skip empty subtrees. */
10603 if (!*tp)
10604 return NULL_TREE;
10606 /* Don't walk the same tree twice, if the user has requested
10607 that we avoid doing so. */
10608 if (pset && pointer_set_insert (pset, *tp))
10609 return NULL_TREE;
10611 /* Call the function. */
10612 walk_subtrees = 1;
10613 result = (*func) (tp, &walk_subtrees, data);
10615 /* If we found something, return it. */
10616 if (result)
10617 return result;
10619 code = TREE_CODE (*tp);
10621 /* Even if we didn't, FUNC may have decided that there was nothing
10622 interesting below this point in the tree. */
10623 if (!walk_subtrees)
10625 /* But we still need to check our siblings. */
10626 if (code == TREE_LIST)
10627 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10628 else if (code == OMP_CLAUSE)
10629 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10630 else
10631 return NULL_TREE;
10634 if (lh)
10636 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10637 if (result || !walk_subtrees)
10638 return result;
10641 switch (code)
10643 case ERROR_MARK:
10644 case IDENTIFIER_NODE:
10645 case INTEGER_CST:
10646 case REAL_CST:
10647 case FIXED_CST:
10648 case VECTOR_CST:
10649 case STRING_CST:
10650 case BLOCK:
10651 case PLACEHOLDER_EXPR:
10652 case SSA_NAME:
10653 case FIELD_DECL:
10654 case RESULT_DECL:
10655 /* None of these have subtrees other than those already walked
10656 above. */
10657 break;
10659 case TREE_LIST:
10660 WALK_SUBTREE (TREE_VALUE (*tp));
10661 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10662 break;
10664 case TREE_VEC:
10666 int len = TREE_VEC_LENGTH (*tp);
10668 if (len == 0)
10669 break;
10671 /* Walk all elements but the first. */
10672 while (--len)
10673 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10675 /* Now walk the first one as a tail call. */
10676 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10679 case COMPLEX_CST:
10680 WALK_SUBTREE (TREE_REALPART (*tp));
10681 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10683 case CONSTRUCTOR:
10685 unsigned HOST_WIDE_INT idx;
10686 constructor_elt *ce;
10688 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10689 WALK_SUBTREE (ce->value);
10691 break;
10693 case SAVE_EXPR:
10694 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10696 case BIND_EXPR:
10698 tree decl;
10699 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10701 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10702 into declarations that are just mentioned, rather than
10703 declared; they don't really belong to this part of the tree.
10704 And, we can see cycles: the initializer for a declaration
10705 can refer to the declaration itself. */
10706 WALK_SUBTREE (DECL_INITIAL (decl));
10707 WALK_SUBTREE (DECL_SIZE (decl));
10708 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10710 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10713 case STATEMENT_LIST:
10715 tree_stmt_iterator i;
10716 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10717 WALK_SUBTREE (*tsi_stmt_ptr (i));
10719 break;
10721 case OMP_CLAUSE:
10722 switch (OMP_CLAUSE_CODE (*tp))
10724 case OMP_CLAUSE_PRIVATE:
10725 case OMP_CLAUSE_SHARED:
10726 case OMP_CLAUSE_FIRSTPRIVATE:
10727 case OMP_CLAUSE_COPYIN:
10728 case OMP_CLAUSE_COPYPRIVATE:
10729 case OMP_CLAUSE_FINAL:
10730 case OMP_CLAUSE_IF:
10731 case OMP_CLAUSE_NUM_THREADS:
10732 case OMP_CLAUSE_SCHEDULE:
10733 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10734 /* FALLTHRU */
10736 case OMP_CLAUSE_NOWAIT:
10737 case OMP_CLAUSE_ORDERED:
10738 case OMP_CLAUSE_DEFAULT:
10739 case OMP_CLAUSE_UNTIED:
10740 case OMP_CLAUSE_MERGEABLE:
10741 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10743 case OMP_CLAUSE_LASTPRIVATE:
10744 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10745 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10746 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10748 case OMP_CLAUSE_COLLAPSE:
10750 int i;
10751 for (i = 0; i < 3; i++)
10752 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10753 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10756 case OMP_CLAUSE_REDUCTION:
10758 int i;
10759 for (i = 0; i < 4; i++)
10760 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10761 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10764 default:
10765 gcc_unreachable ();
10767 break;
10769 case TARGET_EXPR:
10771 int i, len;
10773 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10774 But, we only want to walk once. */
10775 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10776 for (i = 0; i < len; ++i)
10777 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10778 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10781 case DECL_EXPR:
10782 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10783 defining. We only want to walk into these fields of a type in this
10784 case and not in the general case of a mere reference to the type.
10786 The criterion is as follows: if the field can be an expression, it
10787 must be walked only here. This should be in keeping with the fields
10788 that are directly gimplified in gimplify_type_sizes in order for the
10789 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10790 variable-sized types.
10792 Note that DECLs get walked as part of processing the BIND_EXPR. */
10793 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10795 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10796 if (TREE_CODE (*type_p) == ERROR_MARK)
10797 return NULL_TREE;
10799 /* Call the function for the type. See if it returns anything or
10800 doesn't want us to continue. If we are to continue, walk both
10801 the normal fields and those for the declaration case. */
10802 result = (*func) (type_p, &walk_subtrees, data);
10803 if (result || !walk_subtrees)
10804 return result;
10806 /* But do not walk a pointed-to type since it may itself need to
10807 be walked in the declaration case if it isn't anonymous. */
10808 if (!POINTER_TYPE_P (*type_p))
10810 result = walk_type_fields (*type_p, func, data, pset, lh);
10811 if (result)
10812 return result;
10815 /* If this is a record type, also walk the fields. */
10816 if (RECORD_OR_UNION_TYPE_P (*type_p))
10818 tree field;
10820 for (field = TYPE_FIELDS (*type_p); field;
10821 field = DECL_CHAIN (field))
10823 /* We'd like to look at the type of the field, but we can
10824 easily get infinite recursion. So assume it's pointed
10825 to elsewhere in the tree. Also, ignore things that
10826 aren't fields. */
10827 if (TREE_CODE (field) != FIELD_DECL)
10828 continue;
10830 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10831 WALK_SUBTREE (DECL_SIZE (field));
10832 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10833 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10834 WALK_SUBTREE (DECL_QUALIFIER (field));
10838 /* Same for scalar types. */
10839 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10840 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10841 || TREE_CODE (*type_p) == INTEGER_TYPE
10842 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10843 || TREE_CODE (*type_p) == REAL_TYPE)
10845 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10846 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10849 WALK_SUBTREE (TYPE_SIZE (*type_p));
10850 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10852 /* FALLTHRU */
10854 default:
10855 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10857 int i, len;
10859 /* Walk over all the sub-trees of this operand. */
10860 len = TREE_OPERAND_LENGTH (*tp);
10862 /* Go through the subtrees. We need to do this in forward order so
10863 that the scope of a FOR_EXPR is handled properly. */
10864 if (len)
10866 for (i = 0; i < len - 1; ++i)
10867 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10868 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10871 /* If this is a type, walk the needed fields in the type. */
10872 else if (TYPE_P (*tp))
10873 return walk_type_fields (*tp, func, data, pset, lh);
10874 break;
10877 /* We didn't find what we were looking for. */
10878 return NULL_TREE;
10880 #undef WALK_SUBTREE_TAIL
10882 #undef WALK_SUBTREE
10884 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10886 tree
10887 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10888 walk_tree_lh lh)
10890 tree result;
10891 struct pointer_set_t *pset;
10893 pset = pointer_set_create ();
10894 result = walk_tree_1 (tp, func, data, pset, lh);
10895 pointer_set_destroy (pset);
10896 return result;
10900 tree
10901 tree_block (tree t)
10903 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10905 if (IS_EXPR_CODE_CLASS (c))
10906 return LOCATION_BLOCK (t->exp.locus);
10907 gcc_unreachable ();
10908 return NULL;
10911 void
10912 tree_set_block (tree t, tree b)
10914 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10916 if (IS_EXPR_CODE_CLASS (c))
10918 if (b)
10919 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
10920 else
10921 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
10923 else
10924 gcc_unreachable ();
10927 /* Create a nameless artificial label and put it in the current
10928 function context. The label has a location of LOC. Returns the
10929 newly created label. */
10931 tree
10932 create_artificial_label (location_t loc)
10934 tree lab = build_decl (loc,
10935 LABEL_DECL, NULL_TREE, void_type_node);
10937 DECL_ARTIFICIAL (lab) = 1;
10938 DECL_IGNORED_P (lab) = 1;
10939 DECL_CONTEXT (lab) = current_function_decl;
10940 return lab;
10943 /* Given a tree, try to return a useful variable name that we can use
10944 to prefix a temporary that is being assigned the value of the tree.
10945 I.E. given <temp> = &A, return A. */
10947 const char *
10948 get_name (tree t)
10950 tree stripped_decl;
10952 stripped_decl = t;
10953 STRIP_NOPS (stripped_decl);
10954 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10955 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10956 else if (TREE_CODE (stripped_decl) == SSA_NAME)
10958 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
10959 if (!name)
10960 return NULL;
10961 return IDENTIFIER_POINTER (name);
10963 else
10965 switch (TREE_CODE (stripped_decl))
10967 case ADDR_EXPR:
10968 return get_name (TREE_OPERAND (stripped_decl, 0));
10969 default:
10970 return NULL;
10975 /* Return true if TYPE has a variable argument list. */
10977 bool
10978 stdarg_p (const_tree fntype)
10980 function_args_iterator args_iter;
10981 tree n = NULL_TREE, t;
10983 if (!fntype)
10984 return false;
10986 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
10988 n = t;
10991 return n != NULL_TREE && n != void_type_node;
10994 /* Return true if TYPE has a prototype. */
10996 bool
10997 prototype_p (tree fntype)
10999 tree t;
11001 gcc_assert (fntype != NULL_TREE);
11003 t = TYPE_ARG_TYPES (fntype);
11004 return (t != NULL_TREE);
11007 /* If BLOCK is inlined from an __attribute__((__artificial__))
11008 routine, return pointer to location from where it has been
11009 called. */
11010 location_t *
11011 block_nonartificial_location (tree block)
11013 location_t *ret = NULL;
11015 while (block && TREE_CODE (block) == BLOCK
11016 && BLOCK_ABSTRACT_ORIGIN (block))
11018 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11020 while (TREE_CODE (ao) == BLOCK
11021 && BLOCK_ABSTRACT_ORIGIN (ao)
11022 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11023 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11025 if (TREE_CODE (ao) == FUNCTION_DECL)
11027 /* If AO is an artificial inline, point RET to the
11028 call site locus at which it has been inlined and continue
11029 the loop, in case AO's caller is also an artificial
11030 inline. */
11031 if (DECL_DECLARED_INLINE_P (ao)
11032 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11033 ret = &BLOCK_SOURCE_LOCATION (block);
11034 else
11035 break;
11037 else if (TREE_CODE (ao) != BLOCK)
11038 break;
11040 block = BLOCK_SUPERCONTEXT (block);
11042 return ret;
11046 /* If EXP is inlined from an __attribute__((__artificial__))
11047 function, return the location of the original call expression. */
11049 location_t
11050 tree_nonartificial_location (tree exp)
11052 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11054 if (loc)
11055 return *loc;
11056 else
11057 return EXPR_LOCATION (exp);
11061 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11062 nodes. */
11064 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11066 static hashval_t
11067 cl_option_hash_hash (const void *x)
11069 const_tree const t = (const_tree) x;
11070 const char *p;
11071 size_t i;
11072 size_t len = 0;
11073 hashval_t hash = 0;
11075 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11077 p = (const char *)TREE_OPTIMIZATION (t);
11078 len = sizeof (struct cl_optimization);
11081 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11083 p = (const char *)TREE_TARGET_OPTION (t);
11084 len = sizeof (struct cl_target_option);
11087 else
11088 gcc_unreachable ();
11090 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11091 something else. */
11092 for (i = 0; i < len; i++)
11093 if (p[i])
11094 hash = (hash << 4) ^ ((i << 2) | p[i]);
11096 return hash;
11099 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11100 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11101 same. */
11103 static int
11104 cl_option_hash_eq (const void *x, const void *y)
11106 const_tree const xt = (const_tree) x;
11107 const_tree const yt = (const_tree) y;
11108 const char *xp;
11109 const char *yp;
11110 size_t len;
11112 if (TREE_CODE (xt) != TREE_CODE (yt))
11113 return 0;
11115 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11117 xp = (const char *)TREE_OPTIMIZATION (xt);
11118 yp = (const char *)TREE_OPTIMIZATION (yt);
11119 len = sizeof (struct cl_optimization);
11122 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11124 xp = (const char *)TREE_TARGET_OPTION (xt);
11125 yp = (const char *)TREE_TARGET_OPTION (yt);
11126 len = sizeof (struct cl_target_option);
11129 else
11130 gcc_unreachable ();
11132 return (memcmp (xp, yp, len) == 0);
11135 /* Build an OPTIMIZATION_NODE based on the current options. */
11137 tree
11138 build_optimization_node (void)
11140 tree t;
11141 void **slot;
11143 /* Use the cache of optimization nodes. */
11145 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11146 &global_options);
11148 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11149 t = (tree) *slot;
11150 if (!t)
11152 /* Insert this one into the hash table. */
11153 t = cl_optimization_node;
11154 *slot = t;
11156 /* Make a new node for next time round. */
11157 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11160 return t;
11163 /* Build a TARGET_OPTION_NODE based on the current options. */
11165 tree
11166 build_target_option_node (void)
11168 tree t;
11169 void **slot;
11171 /* Use the cache of optimization nodes. */
11173 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11174 &global_options);
11176 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11177 t = (tree) *slot;
11178 if (!t)
11180 /* Insert this one into the hash table. */
11181 t = cl_target_option_node;
11182 *slot = t;
11184 /* Make a new node for next time round. */
11185 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11188 return t;
11191 /* Determine the "ultimate origin" of a block. The block may be an inlined
11192 instance of an inlined instance of a block which is local to an inline
11193 function, so we have to trace all of the way back through the origin chain
11194 to find out what sort of node actually served as the original seed for the
11195 given block. */
11197 tree
11198 block_ultimate_origin (const_tree block)
11200 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11202 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11203 nodes in the function to point to themselves; ignore that if
11204 we're trying to output the abstract instance of this function. */
11205 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11206 return NULL_TREE;
11208 if (immediate_origin == NULL_TREE)
11209 return NULL_TREE;
11210 else
11212 tree ret_val;
11213 tree lookahead = immediate_origin;
11217 ret_val = lookahead;
11218 lookahead = (TREE_CODE (ret_val) == BLOCK
11219 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11221 while (lookahead != NULL && lookahead != ret_val);
11223 /* The block's abstract origin chain may not be the *ultimate* origin of
11224 the block. It could lead to a DECL that has an abstract origin set.
11225 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11226 will give us if it has one). Note that DECL's abstract origins are
11227 supposed to be the most distant ancestor (or so decl_ultimate_origin
11228 claims), so we don't need to loop following the DECL origins. */
11229 if (DECL_P (ret_val))
11230 return DECL_ORIGIN (ret_val);
11232 return ret_val;
11236 /* Return true if T1 and T2 are equivalent lists. */
11238 bool
11239 list_equal_p (const_tree t1, const_tree t2)
11241 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11242 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11243 return false;
11244 return !t1 && !t2;
11247 /* Return true iff conversion in EXP generates no instruction. Mark
11248 it inline so that we fully inline into the stripping functions even
11249 though we have two uses of this function. */
11251 static inline bool
11252 tree_nop_conversion (const_tree exp)
11254 tree outer_type, inner_type;
11256 if (!CONVERT_EXPR_P (exp)
11257 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11258 return false;
11259 if (TREE_OPERAND (exp, 0) == error_mark_node)
11260 return false;
11262 outer_type = TREE_TYPE (exp);
11263 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11265 if (!inner_type)
11266 return false;
11268 /* Use precision rather then machine mode when we can, which gives
11269 the correct answer even for submode (bit-field) types. */
11270 if ((INTEGRAL_TYPE_P (outer_type)
11271 || POINTER_TYPE_P (outer_type)
11272 || TREE_CODE (outer_type) == OFFSET_TYPE)
11273 && (INTEGRAL_TYPE_P (inner_type)
11274 || POINTER_TYPE_P (inner_type)
11275 || TREE_CODE (inner_type) == OFFSET_TYPE))
11276 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11278 /* Otherwise fall back on comparing machine modes (e.g. for
11279 aggregate types, floats). */
11280 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11283 /* Return true iff conversion in EXP generates no instruction. Don't
11284 consider conversions changing the signedness. */
11286 static bool
11287 tree_sign_nop_conversion (const_tree exp)
11289 tree outer_type, inner_type;
11291 if (!tree_nop_conversion (exp))
11292 return false;
11294 outer_type = TREE_TYPE (exp);
11295 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11297 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11298 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11301 /* Strip conversions from EXP according to tree_nop_conversion and
11302 return the resulting expression. */
11304 tree
11305 tree_strip_nop_conversions (tree exp)
11307 while (tree_nop_conversion (exp))
11308 exp = TREE_OPERAND (exp, 0);
11309 return exp;
11312 /* Strip conversions from EXP according to tree_sign_nop_conversion
11313 and return the resulting expression. */
11315 tree
11316 tree_strip_sign_nop_conversions (tree exp)
11318 while (tree_sign_nop_conversion (exp))
11319 exp = TREE_OPERAND (exp, 0);
11320 return exp;
11323 /* Avoid any floating point extensions from EXP. */
11324 tree
11325 strip_float_extensions (tree exp)
11327 tree sub, expt, subt;
11329 /* For floating point constant look up the narrowest type that can hold
11330 it properly and handle it like (type)(narrowest_type)constant.
11331 This way we can optimize for instance a=a*2.0 where "a" is float
11332 but 2.0 is double constant. */
11333 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11335 REAL_VALUE_TYPE orig;
11336 tree type = NULL;
11338 orig = TREE_REAL_CST (exp);
11339 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11340 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11341 type = float_type_node;
11342 else if (TYPE_PRECISION (TREE_TYPE (exp))
11343 > TYPE_PRECISION (double_type_node)
11344 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11345 type = double_type_node;
11346 if (type)
11347 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11350 if (!CONVERT_EXPR_P (exp))
11351 return exp;
11353 sub = TREE_OPERAND (exp, 0);
11354 subt = TREE_TYPE (sub);
11355 expt = TREE_TYPE (exp);
11357 if (!FLOAT_TYPE_P (subt))
11358 return exp;
11360 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11361 return exp;
11363 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11364 return exp;
11366 return strip_float_extensions (sub);
11369 /* Strip out all handled components that produce invariant
11370 offsets. */
11372 const_tree
11373 strip_invariant_refs (const_tree op)
11375 while (handled_component_p (op))
11377 switch (TREE_CODE (op))
11379 case ARRAY_REF:
11380 case ARRAY_RANGE_REF:
11381 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11382 || TREE_OPERAND (op, 2) != NULL_TREE
11383 || TREE_OPERAND (op, 3) != NULL_TREE)
11384 return NULL;
11385 break;
11387 case COMPONENT_REF:
11388 if (TREE_OPERAND (op, 2) != NULL_TREE)
11389 return NULL;
11390 break;
11392 default:;
11394 op = TREE_OPERAND (op, 0);
11397 return op;
11400 static GTY(()) tree gcc_eh_personality_decl;
11402 /* Return the GCC personality function decl. */
11404 tree
11405 lhd_gcc_personality (void)
11407 if (!gcc_eh_personality_decl)
11408 gcc_eh_personality_decl = build_personality_function ("gcc");
11409 return gcc_eh_personality_decl;
11412 /* TARGET is a call target of GIMPLE call statement
11413 (obtained by gimple_call_fn). Return true if it is
11414 OBJ_TYPE_REF representing an virtual call of C++ method.
11415 (As opposed to OBJ_TYPE_REF representing objc calls
11416 through a cast where middle-end devirtualization machinery
11417 can't apply.) */
11419 bool
11420 virtual_method_call_p (tree target)
11422 if (TREE_CODE (target) != OBJ_TYPE_REF)
11423 return false;
11424 target = TREE_TYPE (target);
11425 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11426 target = TREE_TYPE (target);
11427 if (TREE_CODE (target) == FUNCTION_TYPE)
11428 return false;
11429 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11430 return true;
11433 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11435 tree
11436 obj_type_ref_class (tree ref)
11438 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11439 ref = TREE_TYPE (ref);
11440 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11441 ref = TREE_TYPE (ref);
11442 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE);
11443 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11444 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11445 return TREE_TYPE (ref);
11448 /* Try to find a base info of BINFO that would have its field decl at offset
11449 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11450 found, return, otherwise return NULL_TREE. */
11452 tree
11453 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11455 tree type = BINFO_TYPE (binfo);
11457 while (true)
11459 HOST_WIDE_INT pos, size;
11460 tree fld;
11461 int i;
11463 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11464 return binfo;
11465 if (offset < 0)
11466 return NULL_TREE;
11468 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11470 if (TREE_CODE (fld) != FIELD_DECL)
11471 continue;
11473 pos = int_bit_position (fld);
11474 size = tree_low_cst (DECL_SIZE (fld), 1);
11475 if (pos <= offset && (pos + size) > offset)
11476 break;
11478 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11479 return NULL_TREE;
11481 if (!DECL_ARTIFICIAL (fld))
11483 binfo = TYPE_BINFO (TREE_TYPE (fld));
11484 if (!binfo)
11485 return NULL_TREE;
11487 /* Offset 0 indicates the primary base, whose vtable contents are
11488 represented in the binfo for the derived class. */
11489 else if (offset != 0)
11491 tree base_binfo, found_binfo = NULL_TREE;
11492 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11493 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11495 found_binfo = base_binfo;
11496 break;
11498 if (!found_binfo)
11499 return NULL_TREE;
11500 binfo = found_binfo;
11503 type = TREE_TYPE (fld);
11504 offset -= pos;
11508 /* Returns true if X is a typedef decl. */
11510 bool
11511 is_typedef_decl (tree x)
11513 return (x && TREE_CODE (x) == TYPE_DECL
11514 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11517 /* Returns true iff TYPE is a type variant created for a typedef. */
11519 bool
11520 typedef_variant_p (tree type)
11522 return is_typedef_decl (TYPE_NAME (type));
11525 /* Warn about a use of an identifier which was marked deprecated. */
11526 void
11527 warn_deprecated_use (tree node, tree attr)
11529 const char *msg;
11531 if (node == 0 || !warn_deprecated_decl)
11532 return;
11534 if (!attr)
11536 if (DECL_P (node))
11537 attr = DECL_ATTRIBUTES (node);
11538 else if (TYPE_P (node))
11540 tree decl = TYPE_STUB_DECL (node);
11541 if (decl)
11542 attr = lookup_attribute ("deprecated",
11543 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11547 if (attr)
11548 attr = lookup_attribute ("deprecated", attr);
11550 if (attr)
11551 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11552 else
11553 msg = NULL;
11555 if (DECL_P (node))
11557 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11558 if (msg)
11559 warning (OPT_Wdeprecated_declarations,
11560 "%qD is deprecated (declared at %s:%d): %s",
11561 node, xloc.file, xloc.line, msg);
11562 else
11563 warning (OPT_Wdeprecated_declarations,
11564 "%qD is deprecated (declared at %s:%d)",
11565 node, xloc.file, xloc.line);
11567 else if (TYPE_P (node))
11569 tree what = NULL_TREE;
11570 tree decl = TYPE_STUB_DECL (node);
11572 if (TYPE_NAME (node))
11574 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11575 what = TYPE_NAME (node);
11576 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11577 && DECL_NAME (TYPE_NAME (node)))
11578 what = DECL_NAME (TYPE_NAME (node));
11581 if (decl)
11583 expanded_location xloc
11584 = expand_location (DECL_SOURCE_LOCATION (decl));
11585 if (what)
11587 if (msg)
11588 warning (OPT_Wdeprecated_declarations,
11589 "%qE is deprecated (declared at %s:%d): %s",
11590 what, xloc.file, xloc.line, msg);
11591 else
11592 warning (OPT_Wdeprecated_declarations,
11593 "%qE is deprecated (declared at %s:%d)", what,
11594 xloc.file, xloc.line);
11596 else
11598 if (msg)
11599 warning (OPT_Wdeprecated_declarations,
11600 "type is deprecated (declared at %s:%d): %s",
11601 xloc.file, xloc.line, msg);
11602 else
11603 warning (OPT_Wdeprecated_declarations,
11604 "type is deprecated (declared at %s:%d)",
11605 xloc.file, xloc.line);
11608 else
11610 if (what)
11612 if (msg)
11613 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11614 what, msg);
11615 else
11616 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11618 else
11620 if (msg)
11621 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11622 msg);
11623 else
11624 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11630 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
11631 somewhere in it. */
11633 bool
11634 contains_bitfld_component_ref_p (const_tree ref)
11636 while (handled_component_p (ref))
11638 if (TREE_CODE (ref) == COMPONENT_REF
11639 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
11640 return true;
11641 ref = TREE_OPERAND (ref, 0);
11644 return false;
11647 #include "gt-tree.h"