Merge trunk version 198815 into gupc branch.
[official-gcc.git] / gcc / tree.c
blob1095240706a74e839bbc4d788e9bdacf789b8b69
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, tree);
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 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
979 if (TREE_CODE (node) == VAR_DECL)
980 DECL_HAS_DEBUG_EXPR_P (t) = 0;
981 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
983 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
984 DECL_HAS_INIT_PRIORITY_P (t) = 1;
986 if (TREE_CODE (node) == FUNCTION_DECL)
987 DECL_STRUCT_FUNCTION (t) = NULL;
989 else if (TREE_CODE_CLASS (code) == tcc_type)
991 TYPE_UID (t) = next_type_uid++;
992 /* The following is so that the debug code for
993 the copy is different from the original type.
994 The two statements usually duplicate each other
995 (because they clear fields of the same union),
996 but the optimizer should catch that. */
997 TYPE_SYMTAB_POINTER (t) = 0;
998 TYPE_SYMTAB_ADDRESS (t) = 0;
1000 /* Do not copy the values cache. */
1001 if (TYPE_CACHED_VALUES_P(t))
1003 TYPE_CACHED_VALUES_P (t) = 0;
1004 TYPE_CACHED_VALUES (t) = NULL_TREE;
1007 if (TYPE_HAS_BLOCK_FACTOR (node))
1008 SET_TYPE_BLOCK_FACTOR (t, TYPE_BLOCK_FACTOR (node));
1011 return t;
1014 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1015 For example, this can copy a list made of TREE_LIST nodes. */
1017 tree
1018 copy_list (tree list)
1020 tree head;
1021 tree prev, next;
1023 if (list == 0)
1024 return 0;
1026 head = prev = copy_node (list);
1027 next = TREE_CHAIN (list);
1028 while (next)
1030 TREE_CHAIN (prev) = copy_node (next);
1031 prev = TREE_CHAIN (prev);
1032 next = TREE_CHAIN (next);
1034 return head;
1038 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1040 tree
1041 build_int_cst (tree type, HOST_WIDE_INT low)
1043 /* Support legacy code. */
1044 if (!type)
1045 type = integer_type_node;
1047 return double_int_to_tree (type, double_int::from_shwi (low));
1050 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1052 tree
1053 build_int_cst_type (tree type, HOST_WIDE_INT low)
1055 gcc_assert (type);
1057 return double_int_to_tree (type, double_int::from_shwi (low));
1060 /* Constructs tree in type TYPE from with value given by CST. Signedness
1061 of CST is assumed to be the same as the signedness of TYPE. */
1063 tree
1064 double_int_to_tree (tree type, double_int cst)
1066 bool sign_extended_type = !TYPE_UNSIGNED (type);
1068 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1070 return build_int_cst_wide (type, cst.low, cst.high);
1073 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1074 to be the same as the signedness of TYPE. */
1076 bool
1077 double_int_fits_to_tree_p (const_tree type, double_int cst)
1079 bool sign_extended_type = !TYPE_UNSIGNED (type);
1081 double_int ext
1082 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1084 return cst == ext;
1087 /* We force the double_int CST to the range of the type TYPE by sign or
1088 zero extending it. OVERFLOWABLE indicates if we are interested in
1089 overflow of the value, when >0 we are only interested in signed
1090 overflow, for <0 we are interested in any overflow. OVERFLOWED
1091 indicates whether overflow has already occurred. CONST_OVERFLOWED
1092 indicates whether constant overflow has already occurred. We force
1093 T's value to be within range of T's type (by setting to 0 or 1 all
1094 the bits outside the type's range). We set TREE_OVERFLOWED if,
1095 OVERFLOWED is nonzero,
1096 or OVERFLOWABLE is >0 and signed overflow occurs
1097 or OVERFLOWABLE is <0 and any overflow occurs
1098 We return a new tree node for the extended double_int. The node
1099 is shared if no overflow flags are set. */
1102 tree
1103 force_fit_type_double (tree type, double_int cst, int overflowable,
1104 bool overflowed)
1106 bool sign_extended_type = !TYPE_UNSIGNED (type);
1108 /* If we need to set overflow flags, return a new unshared node. */
1109 if (overflowed || !double_int_fits_to_tree_p(type, cst))
1111 if (overflowed
1112 || overflowable < 0
1113 || (overflowable > 0 && sign_extended_type))
1115 tree t = make_node (INTEGER_CST);
1116 TREE_INT_CST (t)
1117 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1118 TREE_TYPE (t) = type;
1119 TREE_OVERFLOW (t) = 1;
1120 return t;
1124 /* Else build a shared node. */
1125 return double_int_to_tree (type, cst);
1128 /* These are the hash table functions for the hash table of INTEGER_CST
1129 nodes of a sizetype. */
1131 /* Return the hash code code X, an INTEGER_CST. */
1133 static hashval_t
1134 int_cst_hash_hash (const void *x)
1136 const_tree const t = (const_tree) x;
1138 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1139 ^ htab_hash_pointer (TREE_TYPE (t)));
1142 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1143 is the same as that given by *Y, which is the same. */
1145 static int
1146 int_cst_hash_eq (const void *x, const void *y)
1148 const_tree const xt = (const_tree) x;
1149 const_tree const yt = (const_tree) y;
1151 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1152 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1153 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1156 /* Create an INT_CST node of TYPE and value HI:LOW.
1157 The returned node is always shared. For small integers we use a
1158 per-type vector cache, for larger ones we use a single hash table. */
1160 tree
1161 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1163 tree t;
1164 int ix = -1;
1165 int limit = 0;
1167 gcc_assert (type);
1169 switch (TREE_CODE (type))
1171 case NULLPTR_TYPE:
1172 gcc_assert (hi == 0 && low == 0);
1173 /* Fallthru. */
1175 case POINTER_TYPE:
1176 case REFERENCE_TYPE:
1177 /* Cache NULL pointer. */
1178 if (!hi && !low)
1180 limit = 1;
1181 ix = 0;
1183 break;
1185 case BOOLEAN_TYPE:
1186 /* Cache false or true. */
1187 limit = 2;
1188 if (!hi && low < 2)
1189 ix = low;
1190 break;
1192 case INTEGER_TYPE:
1193 case OFFSET_TYPE:
1194 if (TYPE_UNSIGNED (type))
1196 /* Cache 0..N */
1197 limit = INTEGER_SHARE_LIMIT;
1198 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1199 ix = low;
1201 else
1203 /* Cache -1..N */
1204 limit = INTEGER_SHARE_LIMIT + 1;
1205 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1206 ix = low + 1;
1207 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1208 ix = 0;
1210 break;
1212 case ENUMERAL_TYPE:
1213 break;
1215 default:
1216 gcc_unreachable ();
1219 if (ix >= 0)
1221 /* Look for it in the type's vector of small shared ints. */
1222 if (!TYPE_CACHED_VALUES_P (type))
1224 TYPE_CACHED_VALUES_P (type) = 1;
1225 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1228 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1229 if (t)
1231 /* Make sure no one is clobbering the shared constant. */
1232 gcc_assert (TREE_TYPE (t) == type);
1233 gcc_assert (TREE_INT_CST_LOW (t) == low);
1234 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1236 else
1238 /* Create a new shared int. */
1239 t = make_node (INTEGER_CST);
1241 TREE_INT_CST_LOW (t) = low;
1242 TREE_INT_CST_HIGH (t) = hi;
1243 TREE_TYPE (t) = type;
1245 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1248 else
1250 /* Use the cache of larger shared ints. */
1251 void **slot;
1253 TREE_INT_CST_LOW (int_cst_node) = low;
1254 TREE_INT_CST_HIGH (int_cst_node) = hi;
1255 TREE_TYPE (int_cst_node) = type;
1257 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1258 t = (tree) *slot;
1259 if (!t)
1261 /* Insert this one into the hash table. */
1262 t = int_cst_node;
1263 *slot = t;
1264 /* Make a new node for next time round. */
1265 int_cst_node = make_node (INTEGER_CST);
1269 return t;
1272 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1273 and the rest are zeros. */
1275 tree
1276 build_low_bits_mask (tree type, unsigned bits)
1278 double_int mask;
1280 gcc_assert (bits <= TYPE_PRECISION (type));
1282 if (bits == TYPE_PRECISION (type)
1283 && !TYPE_UNSIGNED (type))
1284 /* Sign extended all-ones mask. */
1285 mask = double_int_minus_one;
1286 else
1287 mask = double_int::mask (bits);
1289 return build_int_cst_wide (type, mask.low, mask.high);
1292 /* Checks that X is integer constant that can be expressed in (unsigned)
1293 HOST_WIDE_INT without loss of precision. */
1295 bool
1296 cst_and_fits_in_hwi (const_tree x)
1298 if (TREE_CODE (x) != INTEGER_CST)
1299 return false;
1301 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1302 return false;
1304 return (TREE_INT_CST_HIGH (x) == 0
1305 || TREE_INT_CST_HIGH (x) == -1);
1308 /* Build a newly constructed TREE_VEC node of length LEN. */
1310 tree
1311 make_vector_stat (unsigned len MEM_STAT_DECL)
1313 tree t;
1314 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1316 record_node_allocation_statistics (VECTOR_CST, length);
1318 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1320 TREE_SET_CODE (t, VECTOR_CST);
1321 TREE_CONSTANT (t) = 1;
1323 return t;
1326 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1327 are in a list pointed to by VALS. */
1329 tree
1330 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1332 int over = 0;
1333 unsigned cnt = 0;
1334 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1335 TREE_TYPE (v) = type;
1337 /* Iterate through elements and check for overflow. */
1338 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1340 tree value = vals[cnt];
1342 VECTOR_CST_ELT (v, cnt) = value;
1344 /* Don't crash if we get an address constant. */
1345 if (!CONSTANT_CLASS_P (value))
1346 continue;
1348 over |= TREE_OVERFLOW (value);
1351 TREE_OVERFLOW (v) = over;
1352 return v;
1355 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1356 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1358 tree
1359 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1361 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1362 unsigned HOST_WIDE_INT idx;
1363 tree value;
1365 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1366 vec[idx] = value;
1367 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1368 vec[idx] = build_zero_cst (TREE_TYPE (type));
1370 return build_vector (type, vec);
1373 /* Build a vector of type VECTYPE where all the elements are SCs. */
1374 tree
1375 build_vector_from_val (tree vectype, tree sc)
1377 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1379 if (sc == error_mark_node)
1380 return sc;
1382 /* Verify that the vector type is suitable for SC. Note that there
1383 is some inconsistency in the type-system with respect to restrict
1384 qualifications of pointers. Vector types always have a main-variant
1385 element type and the qualification is applied to the vector-type.
1386 So TREE_TYPE (vector-type) does not return a properly qualified
1387 vector element-type. */
1388 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1389 TREE_TYPE (vectype)));
1391 if (CONSTANT_CLASS_P (sc))
1393 tree *v = XALLOCAVEC (tree, nunits);
1394 for (i = 0; i < nunits; ++i)
1395 v[i] = sc;
1396 return build_vector (vectype, v);
1398 else
1400 vec<constructor_elt, va_gc> *v;
1401 vec_alloc (v, nunits);
1402 for (i = 0; i < nunits; ++i)
1403 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1404 return build_constructor (vectype, v);
1408 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1409 are in the vec pointed to by VALS. */
1410 tree
1411 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1413 tree c = make_node (CONSTRUCTOR);
1414 unsigned int i;
1415 constructor_elt *elt;
1416 bool constant_p = true;
1417 bool side_effects_p = false;
1419 TREE_TYPE (c) = type;
1420 CONSTRUCTOR_ELTS (c) = vals;
1422 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1424 /* Mostly ctors will have elts that don't have side-effects, so
1425 the usual case is to scan all the elements. Hence a single
1426 loop for both const and side effects, rather than one loop
1427 each (with early outs). */
1428 if (!TREE_CONSTANT (elt->value))
1429 constant_p = false;
1430 if (TREE_SIDE_EFFECTS (elt->value))
1431 side_effects_p = true;
1434 TREE_SIDE_EFFECTS (c) = side_effects_p;
1435 TREE_CONSTANT (c) = constant_p;
1437 return c;
1440 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1441 INDEX and VALUE. */
1442 tree
1443 build_constructor_single (tree type, tree index, tree value)
1445 vec<constructor_elt, va_gc> *v;
1446 constructor_elt elt = {index, value};
1448 vec_alloc (v, 1);
1449 v->quick_push (elt);
1451 return build_constructor (type, v);
1455 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1456 are in a list pointed to by VALS. */
1457 tree
1458 build_constructor_from_list (tree type, tree vals)
1460 tree t;
1461 vec<constructor_elt, va_gc> *v = NULL;
1463 if (vals)
1465 vec_alloc (v, list_length (vals));
1466 for (t = vals; t; t = TREE_CHAIN (t))
1467 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1470 return build_constructor (type, v);
1473 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1474 of elements, provided as index/value pairs. */
1476 tree
1477 build_constructor_va (tree type, int nelts, ...)
1479 vec<constructor_elt, va_gc> *v = NULL;
1480 va_list p;
1482 va_start (p, nelts);
1483 vec_alloc (v, nelts);
1484 while (nelts--)
1486 tree index = va_arg (p, tree);
1487 tree value = va_arg (p, tree);
1488 CONSTRUCTOR_APPEND_ELT (v, index, value);
1490 va_end (p);
1491 return build_constructor (type, v);
1494 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1496 tree
1497 build_fixed (tree type, FIXED_VALUE_TYPE f)
1499 tree v;
1500 FIXED_VALUE_TYPE *fp;
1502 v = make_node (FIXED_CST);
1503 fp = ggc_alloc_fixed_value ();
1504 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1506 TREE_TYPE (v) = type;
1507 TREE_FIXED_CST_PTR (v) = fp;
1508 return v;
1511 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1513 tree
1514 build_real (tree type, REAL_VALUE_TYPE d)
1516 tree v;
1517 REAL_VALUE_TYPE *dp;
1518 int overflow = 0;
1520 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1521 Consider doing it via real_convert now. */
1523 v = make_node (REAL_CST);
1524 dp = ggc_alloc_real_value ();
1525 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1527 TREE_TYPE (v) = type;
1528 TREE_REAL_CST_PTR (v) = dp;
1529 TREE_OVERFLOW (v) = overflow;
1530 return v;
1533 /* Return a new REAL_CST node whose type is TYPE
1534 and whose value is the integer value of the INTEGER_CST node I. */
1536 REAL_VALUE_TYPE
1537 real_value_from_int_cst (const_tree type, const_tree i)
1539 REAL_VALUE_TYPE d;
1541 /* Clear all bits of the real value type so that we can later do
1542 bitwise comparisons to see if two values are the same. */
1543 memset (&d, 0, sizeof d);
1545 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1546 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1547 TYPE_UNSIGNED (TREE_TYPE (i)));
1548 return d;
1551 /* Given a tree representing an integer constant I, return a tree
1552 representing the same value as a floating-point constant of type TYPE. */
1554 tree
1555 build_real_from_int_cst (tree type, const_tree i)
1557 tree v;
1558 int overflow = TREE_OVERFLOW (i);
1560 v = build_real (type, real_value_from_int_cst (type, i));
1562 TREE_OVERFLOW (v) |= overflow;
1563 return v;
1566 /* Return a newly constructed STRING_CST node whose value is
1567 the LEN characters at STR.
1568 Note that for a C string literal, LEN should include the trailing NUL.
1569 The TREE_TYPE is not initialized. */
1571 tree
1572 build_string (int len, const char *str)
1574 tree s;
1575 size_t length;
1577 /* Do not waste bytes provided by padding of struct tree_string. */
1578 length = len + offsetof (struct tree_string, str) + 1;
1580 record_node_allocation_statistics (STRING_CST, length);
1582 s = ggc_alloc_tree_node (length);
1584 memset (s, 0, sizeof (struct tree_typed));
1585 TREE_SET_CODE (s, STRING_CST);
1586 TREE_CONSTANT (s) = 1;
1587 TREE_STRING_LENGTH (s) = len;
1588 memcpy (s->string.str, str, len);
1589 s->string.str[len] = '\0';
1591 return s;
1594 /* Return a newly constructed COMPLEX_CST node whose value is
1595 specified by the real and imaginary parts REAL and IMAG.
1596 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1597 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1599 tree
1600 build_complex (tree type, tree real, tree imag)
1602 tree t = make_node (COMPLEX_CST);
1604 TREE_REALPART (t) = real;
1605 TREE_IMAGPART (t) = imag;
1606 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1607 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1608 return t;
1611 /* Return a constant of arithmetic type TYPE which is the
1612 multiplicative identity of the set TYPE. */
1614 tree
1615 build_one_cst (tree type)
1617 switch (TREE_CODE (type))
1619 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1620 case POINTER_TYPE: case REFERENCE_TYPE:
1621 case OFFSET_TYPE:
1622 return build_int_cst (type, 1);
1624 case REAL_TYPE:
1625 return build_real (type, dconst1);
1627 case FIXED_POINT_TYPE:
1628 /* We can only generate 1 for accum types. */
1629 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1630 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1632 case VECTOR_TYPE:
1634 tree scalar = build_one_cst (TREE_TYPE (type));
1636 return build_vector_from_val (type, scalar);
1639 case COMPLEX_TYPE:
1640 return build_complex (type,
1641 build_one_cst (TREE_TYPE (type)),
1642 build_zero_cst (TREE_TYPE (type)));
1644 default:
1645 gcc_unreachable ();
1649 /* Return a constant of arithmetic type TYPE which is the
1650 opposite of the multiplicative identity of the set TYPE. */
1652 tree
1653 build_minus_one_cst (tree type)
1655 switch (TREE_CODE (type))
1657 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1658 case POINTER_TYPE: case REFERENCE_TYPE:
1659 case OFFSET_TYPE:
1660 return build_int_cst (type, -1);
1662 case REAL_TYPE:
1663 return build_real (type, dconstm1);
1665 case FIXED_POINT_TYPE:
1666 /* We can only generate 1 for accum types. */
1667 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1668 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1669 TYPE_MODE (type)));
1671 case VECTOR_TYPE:
1673 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1675 return build_vector_from_val (type, scalar);
1678 case COMPLEX_TYPE:
1679 return build_complex (type,
1680 build_minus_one_cst (TREE_TYPE (type)),
1681 build_zero_cst (TREE_TYPE (type)));
1683 default:
1684 gcc_unreachable ();
1688 /* Build 0 constant of type TYPE. This is used by constructor folding
1689 and thus the constant should be represented in memory by
1690 zero(es). */
1692 tree
1693 build_zero_cst (tree type)
1695 switch (TREE_CODE (type))
1697 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1698 case POINTER_TYPE: case REFERENCE_TYPE:
1699 case OFFSET_TYPE: case NULLPTR_TYPE:
1700 return build_int_cst (type, 0);
1702 case REAL_TYPE:
1703 return build_real (type, dconst0);
1705 case FIXED_POINT_TYPE:
1706 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1708 case VECTOR_TYPE:
1710 tree scalar = build_zero_cst (TREE_TYPE (type));
1712 return build_vector_from_val (type, scalar);
1715 case COMPLEX_TYPE:
1717 tree zero = build_zero_cst (TREE_TYPE (type));
1719 return build_complex (type, zero, zero);
1722 default:
1723 if (!AGGREGATE_TYPE_P (type))
1724 return fold_convert (type, integer_zero_node);
1725 return build_constructor (type, NULL);
1730 /* Build a BINFO with LEN language slots. */
1732 tree
1733 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1735 tree t;
1736 size_t length = (offsetof (struct tree_binfo, base_binfos)
1737 + vec<tree, va_gc>::embedded_size (base_binfos));
1739 record_node_allocation_statistics (TREE_BINFO, length);
1741 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1743 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1745 TREE_SET_CODE (t, TREE_BINFO);
1747 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1749 return t;
1752 /* Create a CASE_LABEL_EXPR tree node and return it. */
1754 tree
1755 build_case_label (tree low_value, tree high_value, tree label_decl)
1757 tree t = make_node (CASE_LABEL_EXPR);
1759 TREE_TYPE (t) = void_type_node;
1760 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1762 CASE_LOW (t) = low_value;
1763 CASE_HIGH (t) = high_value;
1764 CASE_LABEL (t) = label_decl;
1765 CASE_CHAIN (t) = NULL_TREE;
1767 return t;
1770 /* Build a newly constructed TREE_VEC node of length LEN. */
1772 tree
1773 make_tree_vec_stat (int len MEM_STAT_DECL)
1775 tree t;
1776 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1778 record_node_allocation_statistics (TREE_VEC, length);
1780 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1782 TREE_SET_CODE (t, TREE_VEC);
1783 TREE_VEC_LENGTH (t) = len;
1785 return t;
1788 /* Return 1 if EXPR is the integer constant zero or a complex constant
1789 of zero. */
1792 integer_zerop (const_tree expr)
1794 STRIP_NOPS (expr);
1796 switch (TREE_CODE (expr))
1798 case INTEGER_CST:
1799 return (TREE_INT_CST_LOW (expr) == 0
1800 && TREE_INT_CST_HIGH (expr) == 0);
1801 case COMPLEX_CST:
1802 return (integer_zerop (TREE_REALPART (expr))
1803 && integer_zerop (TREE_IMAGPART (expr)));
1804 case VECTOR_CST:
1806 unsigned i;
1807 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1808 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1809 return false;
1810 return true;
1812 default:
1813 return false;
1817 /* Return 1 if EXPR is the integer constant one or the corresponding
1818 complex constant. */
1821 integer_onep (const_tree expr)
1823 STRIP_NOPS (expr);
1825 switch (TREE_CODE (expr))
1827 case INTEGER_CST:
1828 return (TREE_INT_CST_LOW (expr) == 1
1829 && TREE_INT_CST_HIGH (expr) == 0);
1830 case COMPLEX_CST:
1831 return (integer_onep (TREE_REALPART (expr))
1832 && integer_zerop (TREE_IMAGPART (expr)));
1833 case VECTOR_CST:
1835 unsigned i;
1836 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1837 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1838 return false;
1839 return true;
1841 default:
1842 return false;
1846 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1847 it contains, or a complex or vector whose subparts are such integers. */
1850 integer_all_onesp (const_tree expr)
1852 int prec;
1853 int uns;
1855 STRIP_NOPS (expr);
1857 if (TREE_CODE (expr) == COMPLEX_CST
1858 && integer_all_onesp (TREE_REALPART (expr))
1859 && integer_all_onesp (TREE_IMAGPART (expr)))
1860 return 1;
1862 else if (TREE_CODE (expr) == VECTOR_CST)
1864 unsigned i;
1865 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1866 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1867 return 0;
1868 return 1;
1871 else if (TREE_CODE (expr) != INTEGER_CST)
1872 return 0;
1874 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1875 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1876 && TREE_INT_CST_HIGH (expr) == -1)
1877 return 1;
1878 if (!uns)
1879 return 0;
1881 prec = TYPE_PRECISION (TREE_TYPE (expr));
1882 if (prec >= HOST_BITS_PER_WIDE_INT)
1884 HOST_WIDE_INT high_value;
1885 int shift_amount;
1887 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1889 /* Can not handle precisions greater than twice the host int size. */
1890 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1891 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1892 /* Shifting by the host word size is undefined according to the ANSI
1893 standard, so we must handle this as a special case. */
1894 high_value = -1;
1895 else
1896 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1898 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1899 && TREE_INT_CST_HIGH (expr) == high_value);
1901 else
1902 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1905 /* Return 1 if EXPR is the integer constant minus one. */
1908 integer_minus_onep (const_tree expr)
1910 STRIP_NOPS (expr);
1912 if (TREE_CODE (expr) == COMPLEX_CST)
1913 return (integer_all_onesp (TREE_REALPART (expr))
1914 && integer_zerop (TREE_IMAGPART (expr)));
1915 else
1916 return integer_all_onesp (expr);
1919 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1920 one bit on). */
1923 integer_pow2p (const_tree expr)
1925 int prec;
1926 unsigned HOST_WIDE_INT high, low;
1928 STRIP_NOPS (expr);
1930 if (TREE_CODE (expr) == COMPLEX_CST
1931 && integer_pow2p (TREE_REALPART (expr))
1932 && integer_zerop (TREE_IMAGPART (expr)))
1933 return 1;
1935 if (TREE_CODE (expr) != INTEGER_CST)
1936 return 0;
1938 prec = TYPE_PRECISION (TREE_TYPE (expr));
1939 high = TREE_INT_CST_HIGH (expr);
1940 low = TREE_INT_CST_LOW (expr);
1942 /* First clear all bits that are beyond the type's precision in case
1943 we've been sign extended. */
1945 if (prec == HOST_BITS_PER_DOUBLE_INT)
1947 else if (prec > HOST_BITS_PER_WIDE_INT)
1948 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1949 else
1951 high = 0;
1952 if (prec < HOST_BITS_PER_WIDE_INT)
1953 low &= ~((HOST_WIDE_INT) (-1) << prec);
1956 if (high == 0 && low == 0)
1957 return 0;
1959 return ((high == 0 && (low & (low - 1)) == 0)
1960 || (low == 0 && (high & (high - 1)) == 0));
1963 /* Return 1 if EXPR is an integer constant other than zero or a
1964 complex constant other than zero. */
1967 integer_nonzerop (const_tree expr)
1969 STRIP_NOPS (expr);
1971 return ((TREE_CODE (expr) == INTEGER_CST
1972 && (TREE_INT_CST_LOW (expr) != 0
1973 || TREE_INT_CST_HIGH (expr) != 0))
1974 || (TREE_CODE (expr) == COMPLEX_CST
1975 && (integer_nonzerop (TREE_REALPART (expr))
1976 || integer_nonzerop (TREE_IMAGPART (expr)))));
1979 /* Return 1 if EXPR is the fixed-point constant zero. */
1982 fixed_zerop (const_tree expr)
1984 return (TREE_CODE (expr) == FIXED_CST
1985 && TREE_FIXED_CST (expr).data.is_zero ());
1988 /* Return the power of two represented by a tree node known to be a
1989 power of two. */
1992 tree_log2 (const_tree expr)
1994 int prec;
1995 HOST_WIDE_INT high, low;
1997 STRIP_NOPS (expr);
1999 if (TREE_CODE (expr) == COMPLEX_CST)
2000 return tree_log2 (TREE_REALPART (expr));
2002 prec = TYPE_PRECISION (TREE_TYPE (expr));
2003 high = TREE_INT_CST_HIGH (expr);
2004 low = TREE_INT_CST_LOW (expr);
2006 /* First clear all bits that are beyond the type's precision in case
2007 we've been sign extended. */
2009 if (prec == HOST_BITS_PER_DOUBLE_INT)
2011 else if (prec > HOST_BITS_PER_WIDE_INT)
2012 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2013 else
2015 high = 0;
2016 if (prec < HOST_BITS_PER_WIDE_INT)
2017 low &= ~((HOST_WIDE_INT) (-1) << prec);
2020 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2021 : exact_log2 (low));
2024 /* Similar, but return the largest integer Y such that 2 ** Y is less
2025 than or equal to EXPR. */
2028 tree_floor_log2 (const_tree expr)
2030 int prec;
2031 HOST_WIDE_INT high, low;
2033 STRIP_NOPS (expr);
2035 if (TREE_CODE (expr) == COMPLEX_CST)
2036 return tree_log2 (TREE_REALPART (expr));
2038 prec = TYPE_PRECISION (TREE_TYPE (expr));
2039 high = TREE_INT_CST_HIGH (expr);
2040 low = TREE_INT_CST_LOW (expr);
2042 /* First clear all bits that are beyond the type's precision in case
2043 we've been sign extended. Ignore if type's precision hasn't been set
2044 since what we are doing is setting it. */
2046 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2048 else if (prec > HOST_BITS_PER_WIDE_INT)
2049 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
2050 else
2052 high = 0;
2053 if (prec < HOST_BITS_PER_WIDE_INT)
2054 low &= ~((HOST_WIDE_INT) (-1) << prec);
2057 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2058 : floor_log2 (low));
2061 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2062 decimal float constants, so don't return 1 for them. */
2065 real_zerop (const_tree expr)
2067 STRIP_NOPS (expr);
2069 switch (TREE_CODE (expr))
2071 case REAL_CST:
2072 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2073 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2074 case COMPLEX_CST:
2075 return real_zerop (TREE_REALPART (expr))
2076 && real_zerop (TREE_IMAGPART (expr));
2077 case VECTOR_CST:
2079 unsigned i;
2080 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2081 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2082 return false;
2083 return true;
2085 default:
2086 return false;
2090 /* Return 1 if EXPR is the real constant one in real or complex form.
2091 Trailing zeroes matter for decimal float constants, so don't return
2092 1 for them. */
2095 real_onep (const_tree expr)
2097 STRIP_NOPS (expr);
2099 switch (TREE_CODE (expr))
2101 case REAL_CST:
2102 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2103 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2104 case COMPLEX_CST:
2105 return real_onep (TREE_REALPART (expr))
2106 && real_zerop (TREE_IMAGPART (expr));
2107 case VECTOR_CST:
2109 unsigned i;
2110 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2111 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2112 return false;
2113 return true;
2115 default:
2116 return false;
2120 /* Return 1 if EXPR is the real constant two. Trailing zeroes matter
2121 for decimal float constants, so don't return 1 for them. */
2124 real_twop (const_tree expr)
2126 STRIP_NOPS (expr);
2128 switch (TREE_CODE (expr))
2130 case REAL_CST:
2131 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2)
2132 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2133 case COMPLEX_CST:
2134 return real_twop (TREE_REALPART (expr))
2135 && real_zerop (TREE_IMAGPART (expr));
2136 case VECTOR_CST:
2138 unsigned i;
2139 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2140 if (!real_twop (VECTOR_CST_ELT (expr, i)))
2141 return false;
2142 return true;
2144 default:
2145 return false;
2149 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2150 matter for decimal float constants, so don't return 1 for them. */
2153 real_minus_onep (const_tree expr)
2155 STRIP_NOPS (expr);
2157 switch (TREE_CODE (expr))
2159 case REAL_CST:
2160 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2161 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2162 case COMPLEX_CST:
2163 return real_minus_onep (TREE_REALPART (expr))
2164 && real_zerop (TREE_IMAGPART (expr));
2165 case VECTOR_CST:
2167 unsigned i;
2168 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2169 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2170 return false;
2171 return true;
2173 default:
2174 return false;
2178 /* Nonzero if EXP is a constant or a cast of a constant. */
2181 really_constant_p (const_tree exp)
2183 /* This is not quite the same as STRIP_NOPS. It does more. */
2184 while (CONVERT_EXPR_P (exp)
2185 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2186 exp = TREE_OPERAND (exp, 0);
2187 return TREE_CONSTANT (exp);
2190 /* Return first list element whose TREE_VALUE is ELEM.
2191 Return 0 if ELEM is not in LIST. */
2193 tree
2194 value_member (tree elem, tree list)
2196 while (list)
2198 if (elem == TREE_VALUE (list))
2199 return list;
2200 list = TREE_CHAIN (list);
2202 return NULL_TREE;
2205 /* Return first list element whose TREE_PURPOSE is ELEM.
2206 Return 0 if ELEM is not in LIST. */
2208 tree
2209 purpose_member (const_tree elem, tree list)
2211 while (list)
2213 if (elem == TREE_PURPOSE (list))
2214 return list;
2215 list = TREE_CHAIN (list);
2217 return NULL_TREE;
2220 /* Return true if ELEM is in V. */
2222 bool
2223 vec_member (const_tree elem, vec<tree, va_gc> *v)
2225 unsigned ix;
2226 tree t;
2227 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2228 if (elem == t)
2229 return true;
2230 return false;
2233 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2234 NULL_TREE. */
2236 tree
2237 chain_index (int idx, tree chain)
2239 for (; chain && idx > 0; --idx)
2240 chain = TREE_CHAIN (chain);
2241 return chain;
2244 /* Return nonzero if ELEM is part of the chain CHAIN. */
2247 chain_member (const_tree elem, const_tree chain)
2249 while (chain)
2251 if (elem == chain)
2252 return 1;
2253 chain = DECL_CHAIN (chain);
2256 return 0;
2259 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2260 We expect a null pointer to mark the end of the chain.
2261 This is the Lisp primitive `length'. */
2264 list_length (const_tree t)
2266 const_tree p = t;
2267 #ifdef ENABLE_TREE_CHECKING
2268 const_tree q = t;
2269 #endif
2270 int len = 0;
2272 while (p)
2274 p = TREE_CHAIN (p);
2275 #ifdef ENABLE_TREE_CHECKING
2276 if (len % 2)
2277 q = TREE_CHAIN (q);
2278 gcc_assert (p != q);
2279 #endif
2280 len++;
2283 return len;
2286 /* Returns the number of FIELD_DECLs in TYPE. */
2289 fields_length (const_tree type)
2291 tree t = TYPE_FIELDS (type);
2292 int count = 0;
2294 for (; t; t = DECL_CHAIN (t))
2295 if (TREE_CODE (t) == FIELD_DECL)
2296 ++count;
2298 return count;
2301 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2302 UNION_TYPE TYPE, or NULL_TREE if none. */
2304 tree
2305 first_field (const_tree type)
2307 tree t = TYPE_FIELDS (type);
2308 while (t && TREE_CODE (t) != FIELD_DECL)
2309 t = TREE_CHAIN (t);
2310 return t;
2313 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2314 by modifying the last node in chain 1 to point to chain 2.
2315 This is the Lisp primitive `nconc'. */
2317 tree
2318 chainon (tree op1, tree op2)
2320 tree t1;
2322 if (!op1)
2323 return op2;
2324 if (!op2)
2325 return op1;
2327 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2328 continue;
2329 TREE_CHAIN (t1) = op2;
2331 #ifdef ENABLE_TREE_CHECKING
2333 tree t2;
2334 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2335 gcc_assert (t2 != t1);
2337 #endif
2339 return op1;
2342 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2344 tree
2345 tree_last (tree chain)
2347 tree next;
2348 if (chain)
2349 while ((next = TREE_CHAIN (chain)))
2350 chain = next;
2351 return chain;
2354 /* Reverse the order of elements in the chain T,
2355 and return the new head of the chain (old last element). */
2357 tree
2358 nreverse (tree t)
2360 tree prev = 0, decl, next;
2361 for (decl = t; decl; decl = next)
2363 /* We shouldn't be using this function to reverse BLOCK chains; we
2364 have blocks_nreverse for that. */
2365 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2366 next = TREE_CHAIN (decl);
2367 TREE_CHAIN (decl) = prev;
2368 prev = decl;
2370 return prev;
2373 /* Return a newly created TREE_LIST node whose
2374 purpose and value fields are PARM and VALUE. */
2376 tree
2377 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2379 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2380 TREE_PURPOSE (t) = parm;
2381 TREE_VALUE (t) = value;
2382 return t;
2385 /* Build a chain of TREE_LIST nodes from a vector. */
2387 tree
2388 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2390 tree ret = NULL_TREE;
2391 tree *pp = &ret;
2392 unsigned int i;
2393 tree t;
2394 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2396 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2397 pp = &TREE_CHAIN (*pp);
2399 return ret;
2402 /* Return a newly created TREE_LIST node whose
2403 purpose and value fields are PURPOSE and VALUE
2404 and whose TREE_CHAIN is CHAIN. */
2406 tree
2407 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2409 tree node;
2411 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2412 memset (node, 0, sizeof (struct tree_common));
2414 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2416 TREE_SET_CODE (node, TREE_LIST);
2417 TREE_CHAIN (node) = chain;
2418 TREE_PURPOSE (node) = purpose;
2419 TREE_VALUE (node) = value;
2420 return node;
2423 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2424 trees. */
2426 vec<tree, va_gc> *
2427 ctor_to_vec (tree ctor)
2429 vec<tree, va_gc> *vec;
2430 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2431 unsigned int ix;
2432 tree val;
2434 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2435 vec->quick_push (val);
2437 return vec;
2440 /* Return the size nominally occupied by an object of type TYPE
2441 when it resides in memory. The value is measured in units of bytes,
2442 and its data type is that normally used for type sizes
2443 (which is the first type created by make_signed_type or
2444 make_unsigned_type). */
2446 tree
2447 size_in_bytes (const_tree type)
2449 tree t;
2451 if (type == error_mark_node)
2452 return integer_zero_node;
2454 type = TYPE_MAIN_VARIANT (type);
2455 t = TYPE_SIZE_UNIT (type);
2457 if (t == 0)
2459 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2460 return size_zero_node;
2463 return t;
2466 /* Return the size of TYPE (in bytes) as a wide integer
2467 or return -1 if the size can vary or is larger than an integer. */
2469 HOST_WIDE_INT
2470 int_size_in_bytes (const_tree type)
2472 tree t;
2474 if (type == error_mark_node)
2475 return 0;
2477 type = TYPE_MAIN_VARIANT (type);
2478 t = TYPE_SIZE_UNIT (type);
2479 if (t == 0
2480 || TREE_CODE (t) != INTEGER_CST
2481 || TREE_INT_CST_HIGH (t) != 0
2482 /* If the result would appear negative, it's too big to represent. */
2483 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2484 return -1;
2486 return TREE_INT_CST_LOW (t);
2489 /* Return the maximum size of TYPE (in bytes) as a wide integer
2490 or return -1 if the size can vary or is larger than an integer. */
2492 HOST_WIDE_INT
2493 max_int_size_in_bytes (const_tree type)
2495 HOST_WIDE_INT size = -1;
2496 tree size_tree;
2498 /* If this is an array type, check for a possible MAX_SIZE attached. */
2500 if (TREE_CODE (type) == ARRAY_TYPE)
2502 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2504 if (size_tree && host_integerp (size_tree, 1))
2505 size = tree_low_cst (size_tree, 1);
2508 /* If we still haven't been able to get a size, see if the language
2509 can compute a maximum size. */
2511 if (size == -1)
2513 size_tree = lang_hooks.types.max_size (type);
2515 if (size_tree && host_integerp (size_tree, 1))
2516 size = tree_low_cst (size_tree, 1);
2519 return size;
2522 /* Returns a tree for the size of EXP in bytes. */
2524 tree
2525 tree_expr_size (const_tree exp)
2527 if (DECL_P (exp)
2528 && DECL_SIZE_UNIT (exp) != 0)
2529 return DECL_SIZE_UNIT (exp);
2530 else
2531 return size_in_bytes (TREE_TYPE (exp));
2534 /* Return the bit position of FIELD, in bits from the start of the record.
2535 This is a tree of type bitsizetype. */
2537 tree
2538 bit_position (const_tree field)
2540 return bit_from_pos (DECL_FIELD_OFFSET (field),
2541 DECL_FIELD_BIT_OFFSET (field));
2544 /* Likewise, but return as an integer. It must be representable in
2545 that way (since it could be a signed value, we don't have the
2546 option of returning -1 like int_size_in_byte can. */
2548 HOST_WIDE_INT
2549 int_bit_position (const_tree field)
2551 return tree_low_cst (bit_position (field), 0);
2554 /* Return the byte position of FIELD, in bytes from the start of the record.
2555 This is a tree of type sizetype. */
2557 tree
2558 byte_position (const_tree field)
2560 return byte_from_pos (DECL_FIELD_OFFSET (field),
2561 DECL_FIELD_BIT_OFFSET (field));
2564 /* Likewise, but return as an integer. It must be representable in
2565 that way (since it could be a signed value, we don't have the
2566 option of returning -1 like int_size_in_byte can. */
2568 HOST_WIDE_INT
2569 int_byte_position (const_tree field)
2571 return tree_low_cst (byte_position (field), 0);
2574 /* Return the strictest alignment, in bits, that T is known to have. */
2576 unsigned int
2577 expr_align (const_tree t)
2579 unsigned int align0, align1;
2581 switch (TREE_CODE (t))
2583 CASE_CONVERT: case NON_LVALUE_EXPR:
2584 /* If we have conversions, we know that the alignment of the
2585 object must meet each of the alignments of the types. */
2586 align0 = expr_align (TREE_OPERAND (t, 0));
2587 align1 = TYPE_ALIGN (TREE_TYPE (t));
2588 return MAX (align0, align1);
2590 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2591 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2592 case CLEANUP_POINT_EXPR:
2593 /* These don't change the alignment of an object. */
2594 return expr_align (TREE_OPERAND (t, 0));
2596 case COND_EXPR:
2597 /* The best we can do is say that the alignment is the least aligned
2598 of the two arms. */
2599 align0 = expr_align (TREE_OPERAND (t, 1));
2600 align1 = expr_align (TREE_OPERAND (t, 2));
2601 return MIN (align0, align1);
2603 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2604 meaningfully, it's always 1. */
2605 case LABEL_DECL: case CONST_DECL:
2606 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2607 case FUNCTION_DECL:
2608 gcc_assert (DECL_ALIGN (t) != 0);
2609 return DECL_ALIGN (t);
2611 default:
2612 break;
2615 /* Otherwise take the alignment from that of the type. */
2616 return TYPE_ALIGN (TREE_TYPE (t));
2619 /* Return, as a tree node, the number of elements for TYPE (which is an
2620 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2622 tree
2623 array_type_nelts (const_tree type)
2625 tree index_type, min, max;
2627 /* If they did it with unspecified bounds, then we should have already
2628 given an error about it before we got here. */
2629 if (! TYPE_DOMAIN (type))
2630 return error_mark_node;
2632 index_type = TYPE_DOMAIN (type);
2633 min = TYPE_MIN_VALUE (index_type);
2634 max = TYPE_MAX_VALUE (index_type);
2636 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2637 if (!max)
2638 return error_mark_node;
2640 return (integer_zerop (min)
2641 ? max
2642 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2645 /* If arg is static -- a reference to an object in static storage -- then
2646 return the object. This is not the same as the C meaning of `static'.
2647 If arg isn't static, return NULL. */
2649 tree
2650 staticp (tree arg)
2652 switch (TREE_CODE (arg))
2654 case FUNCTION_DECL:
2655 /* Nested functions are static, even though taking their address will
2656 involve a trampoline as we unnest the nested function and create
2657 the trampoline on the tree level. */
2658 return arg;
2660 case VAR_DECL:
2661 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2662 && ! DECL_THREAD_LOCAL_P (arg)
2663 && ! DECL_DLLIMPORT_P (arg)
2664 ? arg : NULL);
2666 case CONST_DECL:
2667 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2668 ? arg : NULL);
2670 case CONSTRUCTOR:
2671 return TREE_STATIC (arg) ? arg : NULL;
2673 case LABEL_DECL:
2674 case STRING_CST:
2675 return arg;
2677 case COMPONENT_REF:
2678 /* If the thing being referenced is not a field, then it is
2679 something language specific. */
2680 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2682 /* If we are referencing a bitfield, we can't evaluate an
2683 ADDR_EXPR at compile time and so it isn't a constant. */
2684 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2685 return NULL;
2687 return staticp (TREE_OPERAND (arg, 0));
2689 case BIT_FIELD_REF:
2690 return NULL;
2692 case INDIRECT_REF:
2693 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2695 case ARRAY_REF:
2696 case ARRAY_RANGE_REF:
2697 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2698 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2699 return staticp (TREE_OPERAND (arg, 0));
2700 else
2701 return NULL;
2703 case COMPOUND_LITERAL_EXPR:
2704 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2706 default:
2707 return NULL;
2714 /* Return whether OP is a DECL whose address is function-invariant. */
2716 bool
2717 decl_address_invariant_p (const_tree op)
2719 /* The conditions below are slightly less strict than the one in
2720 staticp. */
2722 switch (TREE_CODE (op))
2724 case PARM_DECL:
2725 case RESULT_DECL:
2726 case LABEL_DECL:
2727 case FUNCTION_DECL:
2728 return true;
2730 case VAR_DECL:
2731 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2732 || DECL_THREAD_LOCAL_P (op)
2733 || DECL_CONTEXT (op) == current_function_decl
2734 || decl_function_context (op) == current_function_decl)
2735 return true;
2736 break;
2738 case CONST_DECL:
2739 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2740 || decl_function_context (op) == current_function_decl)
2741 return true;
2742 break;
2744 default:
2745 break;
2748 return false;
2751 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2753 bool
2754 decl_address_ip_invariant_p (const_tree op)
2756 /* The conditions below are slightly less strict than the one in
2757 staticp. */
2759 switch (TREE_CODE (op))
2761 case LABEL_DECL:
2762 case FUNCTION_DECL:
2763 case STRING_CST:
2764 return true;
2766 case VAR_DECL:
2767 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2768 && !DECL_DLLIMPORT_P (op))
2769 || DECL_THREAD_LOCAL_P (op))
2770 return true;
2771 break;
2773 case CONST_DECL:
2774 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2775 return true;
2776 break;
2778 default:
2779 break;
2782 return false;
2786 /* Return true if T is function-invariant (internal function, does
2787 not handle arithmetic; that's handled in skip_simple_arithmetic and
2788 tree_invariant_p). */
2790 static bool tree_invariant_p (tree t);
2792 static bool
2793 tree_invariant_p_1 (tree t)
2795 tree op;
2797 if (TREE_CONSTANT (t)
2798 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2799 return true;
2801 switch (TREE_CODE (t))
2803 case SAVE_EXPR:
2804 return true;
2806 case ADDR_EXPR:
2807 op = TREE_OPERAND (t, 0);
2808 while (handled_component_p (op))
2810 switch (TREE_CODE (op))
2812 case ARRAY_REF:
2813 case ARRAY_RANGE_REF:
2814 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2815 || TREE_OPERAND (op, 2) != NULL_TREE
2816 || TREE_OPERAND (op, 3) != NULL_TREE)
2817 return false;
2818 break;
2820 case COMPONENT_REF:
2821 if (TREE_OPERAND (op, 2) != NULL_TREE)
2822 return false;
2823 break;
2825 default:;
2827 op = TREE_OPERAND (op, 0);
2830 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2832 default:
2833 break;
2836 return false;
2839 /* Return true if T is function-invariant. */
2841 static bool
2842 tree_invariant_p (tree t)
2844 tree inner = skip_simple_arithmetic (t);
2845 return tree_invariant_p_1 (inner);
2848 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2849 Do this to any expression which may be used in more than one place,
2850 but must be evaluated only once.
2852 Normally, expand_expr would reevaluate the expression each time.
2853 Calling save_expr produces something that is evaluated and recorded
2854 the first time expand_expr is called on it. Subsequent calls to
2855 expand_expr just reuse the recorded value.
2857 The call to expand_expr that generates code that actually computes
2858 the value is the first call *at compile time*. Subsequent calls
2859 *at compile time* generate code to use the saved value.
2860 This produces correct result provided that *at run time* control
2861 always flows through the insns made by the first expand_expr
2862 before reaching the other places where the save_expr was evaluated.
2863 You, the caller of save_expr, must make sure this is so.
2865 Constants, and certain read-only nodes, are returned with no
2866 SAVE_EXPR because that is safe. Expressions containing placeholders
2867 are not touched; see tree.def for an explanation of what these
2868 are used for. */
2870 tree
2871 save_expr (tree expr)
2873 tree t = fold (expr);
2874 tree inner;
2876 /* If the tree evaluates to a constant, then we don't want to hide that
2877 fact (i.e. this allows further folding, and direct checks for constants).
2878 However, a read-only object that has side effects cannot be bypassed.
2879 Since it is no problem to reevaluate literals, we just return the
2880 literal node. */
2881 inner = skip_simple_arithmetic (t);
2882 if (TREE_CODE (inner) == ERROR_MARK)
2883 return inner;
2885 if (tree_invariant_p_1 (inner))
2886 return t;
2888 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2889 it means that the size or offset of some field of an object depends on
2890 the value within another field.
2892 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2893 and some variable since it would then need to be both evaluated once and
2894 evaluated more than once. Front-ends must assure this case cannot
2895 happen by surrounding any such subexpressions in their own SAVE_EXPR
2896 and forcing evaluation at the proper time. */
2897 if (contains_placeholder_p (inner))
2898 return t;
2900 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2901 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2903 /* This expression might be placed ahead of a jump to ensure that the
2904 value was computed on both sides of the jump. So make sure it isn't
2905 eliminated as dead. */
2906 TREE_SIDE_EFFECTS (t) = 1;
2907 return t;
2910 /* Look inside EXPR into any simple arithmetic operations. Return the
2911 outermost non-arithmetic or non-invariant node. */
2913 tree
2914 skip_simple_arithmetic (tree expr)
2916 /* We don't care about whether this can be used as an lvalue in this
2917 context. */
2918 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2919 expr = TREE_OPERAND (expr, 0);
2921 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2922 a constant, it will be more efficient to not make another SAVE_EXPR since
2923 it will allow better simplification and GCSE will be able to merge the
2924 computations if they actually occur. */
2925 while (true)
2927 if (UNARY_CLASS_P (expr))
2928 expr = TREE_OPERAND (expr, 0);
2929 else if (BINARY_CLASS_P (expr))
2931 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
2932 expr = TREE_OPERAND (expr, 0);
2933 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
2934 expr = TREE_OPERAND (expr, 1);
2935 else
2936 break;
2938 else
2939 break;
2942 return expr;
2945 /* Look inside EXPR into simple arithmetic operations involving constants.
2946 Return the outermost non-arithmetic or non-constant node. */
2948 tree
2949 skip_simple_constant_arithmetic (tree expr)
2951 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2952 expr = TREE_OPERAND (expr, 0);
2954 while (true)
2956 if (UNARY_CLASS_P (expr))
2957 expr = TREE_OPERAND (expr, 0);
2958 else if (BINARY_CLASS_P (expr))
2960 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
2961 expr = TREE_OPERAND (expr, 0);
2962 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
2963 expr = TREE_OPERAND (expr, 1);
2964 else
2965 break;
2967 else
2968 break;
2971 return expr;
2974 /* Return which tree structure is used by T. */
2976 enum tree_node_structure_enum
2977 tree_node_structure (const_tree t)
2979 const enum tree_code code = TREE_CODE (t);
2980 return tree_node_structure_for_code (code);
2983 /* Set various status flags when building a CALL_EXPR object T. */
2985 static void
2986 process_call_operands (tree t)
2988 bool side_effects = TREE_SIDE_EFFECTS (t);
2989 bool read_only = false;
2990 int i = call_expr_flags (t);
2992 /* Calls have side-effects, except those to const or pure functions. */
2993 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2994 side_effects = true;
2995 /* Propagate TREE_READONLY of arguments for const functions. */
2996 if (i & ECF_CONST)
2997 read_only = true;
2999 if (!side_effects || read_only)
3000 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3002 tree op = TREE_OPERAND (t, i);
3003 if (op && TREE_SIDE_EFFECTS (op))
3004 side_effects = true;
3005 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3006 read_only = false;
3009 TREE_SIDE_EFFECTS (t) = side_effects;
3010 TREE_READONLY (t) = read_only;
3013 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3014 size or offset that depends on a field within a record. */
3016 bool
3017 contains_placeholder_p (const_tree exp)
3019 enum tree_code code;
3021 if (!exp)
3022 return 0;
3024 code = TREE_CODE (exp);
3025 if (code == PLACEHOLDER_EXPR)
3026 return 1;
3028 switch (TREE_CODE_CLASS (code))
3030 case tcc_reference:
3031 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3032 position computations since they will be converted into a
3033 WITH_RECORD_EXPR involving the reference, which will assume
3034 here will be valid. */
3035 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3037 case tcc_exceptional:
3038 if (code == TREE_LIST)
3039 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3040 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3041 break;
3043 case tcc_unary:
3044 case tcc_binary:
3045 case tcc_comparison:
3046 case tcc_expression:
3047 switch (code)
3049 case COMPOUND_EXPR:
3050 /* Ignoring the first operand isn't quite right, but works best. */
3051 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3053 case COND_EXPR:
3054 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3055 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3056 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3058 case SAVE_EXPR:
3059 /* The save_expr function never wraps anything containing
3060 a PLACEHOLDER_EXPR. */
3061 return 0;
3063 default:
3064 break;
3067 switch (TREE_CODE_LENGTH (code))
3069 case 1:
3070 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3071 case 2:
3072 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3073 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3074 default:
3075 return 0;
3078 case tcc_vl_exp:
3079 switch (code)
3081 case CALL_EXPR:
3083 const_tree arg;
3084 const_call_expr_arg_iterator iter;
3085 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3086 if (CONTAINS_PLACEHOLDER_P (arg))
3087 return 1;
3088 return 0;
3090 default:
3091 return 0;
3094 default:
3095 return 0;
3097 return 0;
3100 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3101 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3102 field positions. */
3104 static bool
3105 type_contains_placeholder_1 (const_tree type)
3107 /* If the size contains a placeholder or the parent type (component type in
3108 the case of arrays) type involves a placeholder, this type does. */
3109 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3110 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3111 || (!POINTER_TYPE_P (type)
3112 && TREE_TYPE (type)
3113 && type_contains_placeholder_p (TREE_TYPE (type))))
3114 return true;
3116 /* Now do type-specific checks. Note that the last part of the check above
3117 greatly limits what we have to do below. */
3118 switch (TREE_CODE (type))
3120 case VOID_TYPE:
3121 case COMPLEX_TYPE:
3122 case ENUMERAL_TYPE:
3123 case BOOLEAN_TYPE:
3124 case POINTER_TYPE:
3125 case OFFSET_TYPE:
3126 case REFERENCE_TYPE:
3127 case METHOD_TYPE:
3128 case FUNCTION_TYPE:
3129 case VECTOR_TYPE:
3130 case NULLPTR_TYPE:
3131 return false;
3133 case INTEGER_TYPE:
3134 case REAL_TYPE:
3135 case FIXED_POINT_TYPE:
3136 /* Here we just check the bounds. */
3137 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3138 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3140 case ARRAY_TYPE:
3141 /* We have already checked the component type above, so just check the
3142 domain type. */
3143 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3145 case RECORD_TYPE:
3146 case UNION_TYPE:
3147 case QUAL_UNION_TYPE:
3149 tree field;
3151 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3152 if (TREE_CODE (field) == FIELD_DECL
3153 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3154 || (TREE_CODE (type) == QUAL_UNION_TYPE
3155 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3156 || type_contains_placeholder_p (TREE_TYPE (field))))
3157 return true;
3159 return false;
3162 default:
3163 gcc_unreachable ();
3167 /* Wrapper around above function used to cache its result. */
3169 bool
3170 type_contains_placeholder_p (tree type)
3172 bool result;
3174 /* If the contains_placeholder_bits field has been initialized,
3175 then we know the answer. */
3176 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3177 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3179 /* Indicate that we've seen this type node, and the answer is false.
3180 This is what we want to return if we run into recursion via fields. */
3181 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3183 /* Compute the real value. */
3184 result = type_contains_placeholder_1 (type);
3186 /* Store the real value. */
3187 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3189 return result;
3192 /* Push tree EXP onto vector QUEUE if it is not already present. */
3194 static void
3195 push_without_duplicates (tree exp, vec<tree> *queue)
3197 unsigned int i;
3198 tree iter;
3200 FOR_EACH_VEC_ELT (*queue, i, iter)
3201 if (simple_cst_equal (iter, exp) == 1)
3202 break;
3204 if (!iter)
3205 queue->safe_push (exp);
3208 /* Given a tree EXP, find all occurrences of references to fields
3209 in a PLACEHOLDER_EXPR and place them in vector REFS without
3210 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3211 we assume here that EXP contains only arithmetic expressions
3212 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3213 argument list. */
3215 void
3216 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3218 enum tree_code code = TREE_CODE (exp);
3219 tree inner;
3220 int i;
3222 /* We handle TREE_LIST and COMPONENT_REF separately. */
3223 if (code == TREE_LIST)
3225 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3226 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3228 else if (code == COMPONENT_REF)
3230 for (inner = TREE_OPERAND (exp, 0);
3231 REFERENCE_CLASS_P (inner);
3232 inner = TREE_OPERAND (inner, 0))
3235 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3236 push_without_duplicates (exp, refs);
3237 else
3238 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3240 else
3241 switch (TREE_CODE_CLASS (code))
3243 case tcc_constant:
3244 break;
3246 case tcc_declaration:
3247 /* Variables allocated to static storage can stay. */
3248 if (!TREE_STATIC (exp))
3249 push_without_duplicates (exp, refs);
3250 break;
3252 case tcc_expression:
3253 /* This is the pattern built in ada/make_aligning_type. */
3254 if (code == ADDR_EXPR
3255 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3257 push_without_duplicates (exp, refs);
3258 break;
3261 /* Fall through... */
3263 case tcc_exceptional:
3264 case tcc_unary:
3265 case tcc_binary:
3266 case tcc_comparison:
3267 case tcc_reference:
3268 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3269 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3270 break;
3272 case tcc_vl_exp:
3273 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3274 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3275 break;
3277 default:
3278 gcc_unreachable ();
3282 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3283 return a tree with all occurrences of references to F in a
3284 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3285 CONST_DECLs. Note that we assume here that EXP contains only
3286 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3287 occurring only in their argument list. */
3289 tree
3290 substitute_in_expr (tree exp, tree f, tree r)
3292 enum tree_code code = TREE_CODE (exp);
3293 tree op0, op1, op2, op3;
3294 tree new_tree;
3296 /* We handle TREE_LIST and COMPONENT_REF separately. */
3297 if (code == TREE_LIST)
3299 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3300 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3301 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3302 return exp;
3304 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3306 else if (code == COMPONENT_REF)
3308 tree inner;
3310 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3311 and it is the right field, replace it with R. */
3312 for (inner = TREE_OPERAND (exp, 0);
3313 REFERENCE_CLASS_P (inner);
3314 inner = TREE_OPERAND (inner, 0))
3317 /* The field. */
3318 op1 = TREE_OPERAND (exp, 1);
3320 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3321 return r;
3323 /* If this expression hasn't been completed let, leave it alone. */
3324 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3325 return exp;
3327 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3328 if (op0 == TREE_OPERAND (exp, 0))
3329 return exp;
3331 new_tree
3332 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3334 else
3335 switch (TREE_CODE_CLASS (code))
3337 case tcc_constant:
3338 return exp;
3340 case tcc_declaration:
3341 if (exp == f)
3342 return r;
3343 else
3344 return exp;
3346 case tcc_expression:
3347 if (exp == f)
3348 return r;
3350 /* Fall through... */
3352 case tcc_exceptional:
3353 case tcc_unary:
3354 case tcc_binary:
3355 case tcc_comparison:
3356 case tcc_reference:
3357 switch (TREE_CODE_LENGTH (code))
3359 case 0:
3360 return exp;
3362 case 1:
3363 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3364 if (op0 == TREE_OPERAND (exp, 0))
3365 return exp;
3367 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3368 break;
3370 case 2:
3371 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3372 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3374 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3375 return exp;
3377 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3378 break;
3380 case 3:
3381 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3382 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3383 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3385 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3386 && op2 == TREE_OPERAND (exp, 2))
3387 return exp;
3389 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3390 break;
3392 case 4:
3393 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3394 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3395 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3396 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3398 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3399 && op2 == TREE_OPERAND (exp, 2)
3400 && op3 == TREE_OPERAND (exp, 3))
3401 return exp;
3403 new_tree
3404 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3405 break;
3407 default:
3408 gcc_unreachable ();
3410 break;
3412 case tcc_vl_exp:
3414 int i;
3416 new_tree = NULL_TREE;
3418 /* If we are trying to replace F with a constant, inline back
3419 functions which do nothing else than computing a value from
3420 the arguments they are passed. This makes it possible to
3421 fold partially or entirely the replacement expression. */
3422 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3424 tree t = maybe_inline_call_in_expr (exp);
3425 if (t)
3426 return SUBSTITUTE_IN_EXPR (t, f, r);
3429 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3431 tree op = TREE_OPERAND (exp, i);
3432 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3433 if (new_op != op)
3435 if (!new_tree)
3436 new_tree = copy_node (exp);
3437 TREE_OPERAND (new_tree, i) = new_op;
3441 if (new_tree)
3443 new_tree = fold (new_tree);
3444 if (TREE_CODE (new_tree) == CALL_EXPR)
3445 process_call_operands (new_tree);
3447 else
3448 return exp;
3450 break;
3452 default:
3453 gcc_unreachable ();
3456 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3458 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3459 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3461 return new_tree;
3464 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3465 for it within OBJ, a tree that is an object or a chain of references. */
3467 tree
3468 substitute_placeholder_in_expr (tree exp, tree obj)
3470 enum tree_code code = TREE_CODE (exp);
3471 tree op0, op1, op2, op3;
3472 tree new_tree;
3474 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3475 in the chain of OBJ. */
3476 if (code == PLACEHOLDER_EXPR)
3478 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3479 tree elt;
3481 for (elt = obj; elt != 0;
3482 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3483 || TREE_CODE (elt) == COND_EXPR)
3484 ? TREE_OPERAND (elt, 1)
3485 : (REFERENCE_CLASS_P (elt)
3486 || UNARY_CLASS_P (elt)
3487 || BINARY_CLASS_P (elt)
3488 || VL_EXP_CLASS_P (elt)
3489 || EXPRESSION_CLASS_P (elt))
3490 ? TREE_OPERAND (elt, 0) : 0))
3491 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3492 return elt;
3494 for (elt = obj; elt != 0;
3495 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3496 || TREE_CODE (elt) == COND_EXPR)
3497 ? TREE_OPERAND (elt, 1)
3498 : (REFERENCE_CLASS_P (elt)
3499 || UNARY_CLASS_P (elt)
3500 || BINARY_CLASS_P (elt)
3501 || VL_EXP_CLASS_P (elt)
3502 || EXPRESSION_CLASS_P (elt))
3503 ? TREE_OPERAND (elt, 0) : 0))
3504 if (POINTER_TYPE_P (TREE_TYPE (elt))
3505 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3506 == need_type))
3507 return fold_build1 (INDIRECT_REF, need_type, elt);
3509 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3510 survives until RTL generation, there will be an error. */
3511 return exp;
3514 /* TREE_LIST is special because we need to look at TREE_VALUE
3515 and TREE_CHAIN, not TREE_OPERANDS. */
3516 else if (code == TREE_LIST)
3518 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3519 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3520 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3521 return exp;
3523 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3525 else
3526 switch (TREE_CODE_CLASS (code))
3528 case tcc_constant:
3529 case tcc_declaration:
3530 return exp;
3532 case tcc_exceptional:
3533 case tcc_unary:
3534 case tcc_binary:
3535 case tcc_comparison:
3536 case tcc_expression:
3537 case tcc_reference:
3538 case tcc_statement:
3539 switch (TREE_CODE_LENGTH (code))
3541 case 0:
3542 return exp;
3544 case 1:
3545 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3546 if (op0 == TREE_OPERAND (exp, 0))
3547 return exp;
3549 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3550 break;
3552 case 2:
3553 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3554 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3556 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3557 return exp;
3559 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3560 break;
3562 case 3:
3563 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3564 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3565 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3567 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3568 && op2 == TREE_OPERAND (exp, 2))
3569 return exp;
3571 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3572 break;
3574 case 4:
3575 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3576 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3577 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3578 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3580 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3581 && op2 == TREE_OPERAND (exp, 2)
3582 && op3 == TREE_OPERAND (exp, 3))
3583 return exp;
3585 new_tree
3586 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3587 break;
3589 default:
3590 gcc_unreachable ();
3592 break;
3594 case tcc_vl_exp:
3596 int i;
3598 new_tree = NULL_TREE;
3600 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3602 tree op = TREE_OPERAND (exp, i);
3603 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3604 if (new_op != op)
3606 if (!new_tree)
3607 new_tree = copy_node (exp);
3608 TREE_OPERAND (new_tree, i) = new_op;
3612 if (new_tree)
3614 new_tree = fold (new_tree);
3615 if (TREE_CODE (new_tree) == CALL_EXPR)
3616 process_call_operands (new_tree);
3618 else
3619 return exp;
3621 break;
3623 default:
3624 gcc_unreachable ();
3627 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3629 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3630 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3632 return new_tree;
3635 /* Stabilize a reference so that we can use it any number of times
3636 without causing its operands to be evaluated more than once.
3637 Returns the stabilized reference. This works by means of save_expr,
3638 so see the caveats in the comments about save_expr.
3640 Also allows conversion expressions whose operands are references.
3641 Any other kind of expression is returned unchanged. */
3643 tree
3644 stabilize_reference (tree ref)
3646 tree result;
3647 enum tree_code code = TREE_CODE (ref);
3649 switch (code)
3651 case VAR_DECL:
3652 case PARM_DECL:
3653 case RESULT_DECL:
3654 /* No action is needed in this case. */
3655 return ref;
3657 CASE_CONVERT:
3658 case FLOAT_EXPR:
3659 case FIX_TRUNC_EXPR:
3660 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3661 break;
3663 case INDIRECT_REF:
3664 result = build_nt (INDIRECT_REF,
3665 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3666 break;
3668 case COMPONENT_REF:
3669 result = build_nt (COMPONENT_REF,
3670 stabilize_reference (TREE_OPERAND (ref, 0)),
3671 TREE_OPERAND (ref, 1), NULL_TREE);
3672 break;
3674 case BIT_FIELD_REF:
3675 result = build_nt (BIT_FIELD_REF,
3676 stabilize_reference (TREE_OPERAND (ref, 0)),
3677 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3678 break;
3680 case ARRAY_REF:
3681 result = build_nt (ARRAY_REF,
3682 stabilize_reference (TREE_OPERAND (ref, 0)),
3683 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3684 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3685 break;
3687 case ARRAY_RANGE_REF:
3688 result = build_nt (ARRAY_RANGE_REF,
3689 stabilize_reference (TREE_OPERAND (ref, 0)),
3690 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3691 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3692 break;
3694 case COMPOUND_EXPR:
3695 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3696 it wouldn't be ignored. This matters when dealing with
3697 volatiles. */
3698 return stabilize_reference_1 (ref);
3700 /* If arg isn't a kind of lvalue we recognize, make no change.
3701 Caller should recognize the error for an invalid lvalue. */
3702 default:
3703 return ref;
3705 case ERROR_MARK:
3706 return error_mark_node;
3709 TREE_TYPE (result) = TREE_TYPE (ref);
3710 TREE_READONLY (result) = TREE_READONLY (ref);
3711 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3712 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3713 TREE_SHARED (result) = TREE_SHARED (ref);
3714 TREE_STRICT (result) = TREE_STRICT (ref);
3715 TREE_RELAXED (result) = TREE_RELAXED (ref);
3717 return result;
3720 /* Subroutine of stabilize_reference; this is called for subtrees of
3721 references. Any expression with side-effects must be put in a SAVE_EXPR
3722 to ensure that it is only evaluated once.
3724 We don't put SAVE_EXPR nodes around everything, because assigning very
3725 simple expressions to temporaries causes us to miss good opportunities
3726 for optimizations. Among other things, the opportunity to fold in the
3727 addition of a constant into an addressing mode often gets lost, e.g.
3728 "y[i+1] += x;". In general, we take the approach that we should not make
3729 an assignment unless we are forced into it - i.e., that any non-side effect
3730 operator should be allowed, and that cse should take care of coalescing
3731 multiple utterances of the same expression should that prove fruitful. */
3733 tree
3734 stabilize_reference_1 (tree e)
3736 tree result;
3737 enum tree_code code = TREE_CODE (e);
3739 /* We cannot ignore const expressions because it might be a reference
3740 to a const array but whose index contains side-effects. But we can
3741 ignore things that are actual constant or that already have been
3742 handled by this function. */
3744 if (tree_invariant_p (e))
3745 return e;
3747 switch (TREE_CODE_CLASS (code))
3749 case tcc_exceptional:
3750 case tcc_type:
3751 case tcc_declaration:
3752 case tcc_comparison:
3753 case tcc_statement:
3754 case tcc_expression:
3755 case tcc_reference:
3756 case tcc_vl_exp:
3757 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3758 so that it will only be evaluated once. */
3759 /* The reference (r) and comparison (<) classes could be handled as
3760 below, but it is generally faster to only evaluate them once. */
3761 if (TREE_SIDE_EFFECTS (e))
3762 return save_expr (e);
3763 return e;
3765 case tcc_constant:
3766 /* Constants need no processing. In fact, we should never reach
3767 here. */
3768 return e;
3770 case tcc_binary:
3771 /* Division is slow and tends to be compiled with jumps,
3772 especially the division by powers of 2 that is often
3773 found inside of an array reference. So do it just once. */
3774 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3775 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3776 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3777 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3778 return save_expr (e);
3779 /* Recursively stabilize each operand. */
3780 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3781 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3782 break;
3784 case tcc_unary:
3785 /* Recursively stabilize each operand. */
3786 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3787 break;
3789 default:
3790 gcc_unreachable ();
3793 TREE_TYPE (result) = TREE_TYPE (e);
3794 TREE_READONLY (result) = TREE_READONLY (e);
3795 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3796 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3797 TREE_SHARED (result) = TREE_SHARED (e);
3798 TREE_STRICT (result) = TREE_STRICT (e);
3799 TREE_RELAXED (result) = TREE_RELAXED (e);
3801 return result;
3804 /* Low-level constructors for expressions. */
3806 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3807 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3809 void
3810 recompute_tree_invariant_for_addr_expr (tree t)
3812 tree node;
3813 bool tc = true, se = false;
3815 /* We started out assuming this address is both invariant and constant, but
3816 does not have side effects. Now go down any handled components and see if
3817 any of them involve offsets that are either non-constant or non-invariant.
3818 Also check for side-effects.
3820 ??? Note that this code makes no attempt to deal with the case where
3821 taking the address of something causes a copy due to misalignment. */
3823 #define UPDATE_FLAGS(NODE) \
3824 do { tree _node = (NODE); \
3825 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3826 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3828 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3829 node = TREE_OPERAND (node, 0))
3831 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3832 array reference (probably made temporarily by the G++ front end),
3833 so ignore all the operands. */
3834 if ((TREE_CODE (node) == ARRAY_REF
3835 || TREE_CODE (node) == ARRAY_RANGE_REF)
3836 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3838 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3839 if (TREE_OPERAND (node, 2))
3840 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3841 if (TREE_OPERAND (node, 3))
3842 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3844 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3845 FIELD_DECL, apparently. The G++ front end can put something else
3846 there, at least temporarily. */
3847 else if (TREE_CODE (node) == COMPONENT_REF
3848 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3850 if (TREE_OPERAND (node, 2))
3851 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3855 node = lang_hooks.expr_to_decl (node, &tc, &se);
3857 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3858 the address, since &(*a)->b is a form of addition. If it's a constant, the
3859 address is constant too. If it's a decl, its address is constant if the
3860 decl is static. Everything else is not constant and, furthermore,
3861 taking the address of a volatile variable is not volatile. */
3862 if (TREE_CODE (node) == INDIRECT_REF
3863 || TREE_CODE (node) == MEM_REF)
3864 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3865 else if (CONSTANT_CLASS_P (node))
3867 else if (DECL_P (node))
3868 tc &= (staticp (node) != NULL_TREE);
3869 else
3871 tc = false;
3872 se |= TREE_SIDE_EFFECTS (node);
3876 TREE_CONSTANT (t) = tc;
3877 TREE_SIDE_EFFECTS (t) = se;
3878 #undef UPDATE_FLAGS
3881 /* Build an expression of code CODE, data type TYPE, and operands as
3882 specified. Expressions and reference nodes can be created this way.
3883 Constants, decls, types and misc nodes cannot be.
3885 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3886 enough for all extant tree codes. */
3888 tree
3889 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3891 tree t;
3893 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3895 t = make_node_stat (code PASS_MEM_STAT);
3896 TREE_TYPE (t) = tt;
3898 return t;
3901 tree
3902 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3904 int length = sizeof (struct tree_exp);
3905 tree t;
3907 record_node_allocation_statistics (code, length);
3909 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3911 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
3913 memset (t, 0, sizeof (struct tree_common));
3915 TREE_SET_CODE (t, code);
3917 TREE_TYPE (t) = type;
3918 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3919 TREE_OPERAND (t, 0) = node;
3920 if (node && !TYPE_P (node))
3922 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3923 TREE_READONLY (t) = TREE_READONLY (node);
3926 if (TREE_CODE_CLASS (code) == tcc_statement)
3927 TREE_SIDE_EFFECTS (t) = 1;
3928 else switch (code)
3930 case VA_ARG_EXPR:
3931 /* All of these have side-effects, no matter what their
3932 operands are. */
3933 TREE_SIDE_EFFECTS (t) = 1;
3934 TREE_READONLY (t) = 0;
3935 break;
3937 case INDIRECT_REF:
3938 /* Whether a dereference is readonly has nothing to do with whether
3939 its operand is readonly. */
3940 TREE_READONLY (t) = 0;
3941 TREE_SHARED (t) = upc_shared_type_p (type);
3942 break;
3944 case ADDR_EXPR:
3945 if (node)
3946 recompute_tree_invariant_for_addr_expr (t);
3947 break;
3949 default:
3950 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3951 && node && !TYPE_P (node)
3952 && TREE_CONSTANT (node))
3953 TREE_CONSTANT (t) = 1;
3954 if (TREE_CODE_CLASS (code) == tcc_reference
3955 && node && TREE_THIS_VOLATILE (node))
3956 TREE_THIS_VOLATILE (t) = 1;
3957 /* Drop the UPC "shared" type qualifier for
3958 expressions involving UPC shared objects. */
3959 if (TREE_CODE_CLASS (code) == tcc_unary
3960 && node && !TYPE_P (node)
3961 && upc_shared_type_p (type))
3962 TREE_TYPE (t) = build_upc_unshared_type (type);
3963 break;
3966 return t;
3969 #define PROCESS_ARG(N) \
3970 do { \
3971 TREE_OPERAND (t, N) = arg##N; \
3972 if (arg##N &&!TYPE_P (arg##N)) \
3974 if (TREE_SIDE_EFFECTS (arg##N)) \
3975 side_effects = 1; \
3976 if (!TREE_READONLY (arg##N) \
3977 && !CONSTANT_CLASS_P (arg##N)) \
3978 (void) (read_only = 0); \
3979 if (!TREE_CONSTANT (arg##N)) \
3980 (void) (constant = 0); \
3982 } while (0)
3984 tree
3985 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3987 bool constant, read_only, side_effects;
3988 tree t;
3990 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3992 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3993 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3994 /* When sizetype precision doesn't match that of pointers
3995 we need to be able to build explicit extensions or truncations
3996 of the offset argument. */
3997 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3998 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3999 && TREE_CODE (arg1) == INTEGER_CST);
4001 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4002 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4003 && ptrofftype_p (TREE_TYPE (arg1)));
4005 t = make_node_stat (code PASS_MEM_STAT);
4007 /* Remove UPC shared type qualifiers from the result type. */
4008 if (upc_shared_type_p (tt))
4009 tt = build_upc_unshared_type (tt);
4010 TREE_TYPE (t) = tt;
4012 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4013 result based on those same flags for the arguments. But if the
4014 arguments aren't really even `tree' expressions, we shouldn't be trying
4015 to do this. */
4017 /* Expressions without side effects may be constant if their
4018 arguments are as well. */
4019 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4020 || TREE_CODE_CLASS (code) == tcc_binary);
4021 read_only = 1;
4022 side_effects = TREE_SIDE_EFFECTS (t);
4024 PROCESS_ARG(0);
4025 PROCESS_ARG(1);
4027 TREE_READONLY (t) = read_only;
4028 TREE_CONSTANT (t) = constant;
4029 TREE_SIDE_EFFECTS (t) = side_effects;
4030 TREE_THIS_VOLATILE (t)
4031 = (TREE_CODE_CLASS (code) == tcc_reference
4032 && arg0 && TREE_THIS_VOLATILE (arg0));
4034 return t;
4038 tree
4039 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4040 tree arg2 MEM_STAT_DECL)
4042 bool constant, read_only, side_effects;
4043 tree t;
4045 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4046 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4048 t = make_node_stat (code PASS_MEM_STAT);
4049 TREE_TYPE (t) = tt;
4051 read_only = 1;
4053 /* As a special exception, if COND_EXPR has NULL branches, we
4054 assume that it is a gimple statement and always consider
4055 it to have side effects. */
4056 if (code == COND_EXPR
4057 && tt == void_type_node
4058 && arg1 == NULL_TREE
4059 && arg2 == NULL_TREE)
4060 side_effects = true;
4061 else
4062 side_effects = TREE_SIDE_EFFECTS (t);
4064 PROCESS_ARG(0);
4065 PROCESS_ARG(1);
4066 PROCESS_ARG(2);
4068 if (code == COND_EXPR)
4069 TREE_READONLY (t) = read_only;
4071 TREE_SIDE_EFFECTS (t) = side_effects;
4072 TREE_THIS_VOLATILE (t)
4073 = (TREE_CODE_CLASS (code) == tcc_reference
4074 && arg0 && TREE_THIS_VOLATILE (arg0));
4075 TREE_SHARED (t)
4076 = (TREE_CODE_CLASS (code) == tcc_reference
4077 && arg0 && TREE_SHARED (arg0));
4078 if (TREE_SHARED (t))
4080 TREE_STRICT (t) = TREE_STRICT (arg0);
4081 TREE_RELAXED (t) = TREE_RELAXED (arg0);
4084 return t;
4087 tree
4088 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4089 tree arg2, tree arg3 MEM_STAT_DECL)
4091 bool constant, read_only, side_effects;
4092 tree t;
4094 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4096 t = make_node_stat (code PASS_MEM_STAT);
4097 TREE_TYPE (t) = tt;
4099 side_effects = TREE_SIDE_EFFECTS (t);
4101 PROCESS_ARG(0);
4102 PROCESS_ARG(1);
4103 PROCESS_ARG(2);
4104 PROCESS_ARG(3);
4106 TREE_SIDE_EFFECTS (t) = side_effects;
4107 TREE_THIS_VOLATILE (t)
4108 = (TREE_CODE_CLASS (code) == tcc_reference
4109 && arg0 && TREE_THIS_VOLATILE (arg0));
4111 return t;
4114 tree
4115 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4116 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4118 bool constant, read_only, side_effects;
4119 tree t;
4121 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4123 t = make_node_stat (code PASS_MEM_STAT);
4124 TREE_TYPE (t) = tt;
4126 side_effects = TREE_SIDE_EFFECTS (t);
4128 PROCESS_ARG(0);
4129 PROCESS_ARG(1);
4130 PROCESS_ARG(2);
4131 PROCESS_ARG(3);
4132 PROCESS_ARG(4);
4134 TREE_SIDE_EFFECTS (t) = side_effects;
4135 TREE_THIS_VOLATILE (t)
4136 = (TREE_CODE_CLASS (code) == tcc_reference
4137 && arg0 && TREE_THIS_VOLATILE (arg0));
4139 return t;
4142 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4143 on the pointer PTR. */
4145 tree
4146 build_simple_mem_ref_loc (location_t loc, tree ptr)
4148 HOST_WIDE_INT offset = 0;
4149 tree ptype = TREE_TYPE (ptr);
4150 tree tem;
4151 /* For convenience allow addresses that collapse to a simple base
4152 and offset. */
4153 if (TREE_CODE (ptr) == ADDR_EXPR
4154 && (handled_component_p (TREE_OPERAND (ptr, 0))
4155 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4157 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4158 gcc_assert (ptr);
4159 ptr = build_fold_addr_expr (ptr);
4160 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4162 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4163 ptr, build_int_cst (ptype, offset));
4164 SET_EXPR_LOCATION (tem, loc);
4165 return tem;
4168 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4170 double_int
4171 mem_ref_offset (const_tree t)
4173 tree toff = TREE_OPERAND (t, 1);
4174 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4177 /* Return the pointer-type relevant for TBAA purposes from the
4178 gimple memory reference tree T. This is the type to be used for
4179 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T. */
4181 tree
4182 reference_alias_ptr_type (const_tree t)
4184 const_tree base = t;
4185 while (handled_component_p (base))
4186 base = TREE_OPERAND (base, 0);
4187 if (TREE_CODE (base) == MEM_REF)
4188 return TREE_TYPE (TREE_OPERAND (base, 1));
4189 else if (TREE_CODE (base) == TARGET_MEM_REF)
4190 return TREE_TYPE (TMR_OFFSET (base));
4191 else
4192 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (base)));
4195 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4196 offsetted by OFFSET units. */
4198 tree
4199 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4201 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4202 build_fold_addr_expr (base),
4203 build_int_cst (ptr_type_node, offset));
4204 tree addr = build1 (ADDR_EXPR, type, ref);
4205 recompute_tree_invariant_for_addr_expr (addr);
4206 return addr;
4209 /* Similar except don't specify the TREE_TYPE
4210 and leave the TREE_SIDE_EFFECTS as 0.
4211 It is permissible for arguments to be null,
4212 or even garbage if their values do not matter. */
4214 tree
4215 build_nt (enum tree_code code, ...)
4217 tree t;
4218 int length;
4219 int i;
4220 va_list p;
4222 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4224 va_start (p, code);
4226 t = make_node (code);
4227 length = TREE_CODE_LENGTH (code);
4229 for (i = 0; i < length; i++)
4230 TREE_OPERAND (t, i) = va_arg (p, tree);
4232 va_end (p);
4233 return t;
4236 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4237 tree vec. */
4239 tree
4240 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4242 tree ret, t;
4243 unsigned int ix;
4245 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4246 CALL_EXPR_FN (ret) = fn;
4247 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4248 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4249 CALL_EXPR_ARG (ret, ix) = t;
4250 return ret;
4253 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4254 We do NOT enter this node in any sort of symbol table.
4256 LOC is the location of the decl.
4258 layout_decl is used to set up the decl's storage layout.
4259 Other slots are initialized to 0 or null pointers. */
4261 tree
4262 build_decl_stat (location_t loc, enum tree_code code, tree name,
4263 tree type MEM_STAT_DECL)
4265 tree t;
4267 t = make_node_stat (code PASS_MEM_STAT);
4268 DECL_SOURCE_LOCATION (t) = loc;
4270 /* if (type == error_mark_node)
4271 type = integer_type_node; */
4272 /* That is not done, deliberately, so that having error_mark_node
4273 as the type can suppress useless errors in the use of this variable. */
4275 DECL_NAME (t) = name;
4276 TREE_TYPE (t) = type;
4278 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4279 layout_decl (t, 0);
4281 return t;
4284 /* Builds and returns function declaration with NAME and TYPE. */
4286 tree
4287 build_fn_decl (const char *name, tree type)
4289 tree id = get_identifier (name);
4290 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4292 DECL_EXTERNAL (decl) = 1;
4293 TREE_PUBLIC (decl) = 1;
4294 DECL_ARTIFICIAL (decl) = 1;
4295 TREE_NOTHROW (decl) = 1;
4297 return decl;
4300 vec<tree, va_gc> *all_translation_units;
4302 /* Builds a new translation-unit decl with name NAME, queues it in the
4303 global list of translation-unit decls and returns it. */
4305 tree
4306 build_translation_unit_decl (tree name)
4308 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4309 name, NULL_TREE);
4310 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4311 vec_safe_push (all_translation_units, tu);
4312 return tu;
4316 /* BLOCK nodes are used to represent the structure of binding contours
4317 and declarations, once those contours have been exited and their contents
4318 compiled. This information is used for outputting debugging info. */
4320 tree
4321 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4323 tree block = make_node (BLOCK);
4325 BLOCK_VARS (block) = vars;
4326 BLOCK_SUBBLOCKS (block) = subblocks;
4327 BLOCK_SUPERCONTEXT (block) = supercontext;
4328 BLOCK_CHAIN (block) = chain;
4329 return block;
4333 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4335 LOC is the location to use in tree T. */
4337 void
4338 protected_set_expr_location (tree t, location_t loc)
4340 if (t && CAN_HAVE_LOCATION_P (t))
4341 SET_EXPR_LOCATION (t, loc);
4344 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4345 is ATTRIBUTE. */
4347 tree
4348 build_decl_attribute_variant (tree ddecl, tree attribute)
4350 DECL_ATTRIBUTES (ddecl) = attribute;
4351 return ddecl;
4354 /* Borrowed from hashtab.c iterative_hash implementation. */
4355 #define mix(a,b,c) \
4357 a -= b; a -= c; a ^= (c>>13); \
4358 b -= c; b -= a; b ^= (a<< 8); \
4359 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4360 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4361 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4362 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4363 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4364 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4365 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4369 /* Produce good hash value combining VAL and VAL2. */
4370 hashval_t
4371 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4373 /* the golden ratio; an arbitrary value. */
4374 hashval_t a = 0x9e3779b9;
4376 mix (a, val, val2);
4377 return val2;
4380 /* Produce good hash value combining VAL and VAL2. */
4381 hashval_t
4382 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4384 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4385 return iterative_hash_hashval_t (val, val2);
4386 else
4388 hashval_t a = (hashval_t) val;
4389 /* Avoid warnings about shifting of more than the width of the type on
4390 hosts that won't execute this path. */
4391 int zero = 0;
4392 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4393 mix (a, b, val2);
4394 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4396 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4397 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4398 mix (a, b, val2);
4400 return val2;
4404 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4405 is ATTRIBUTE and its qualifiers are QUALS.
4407 Record such modified types already made so we don't make duplicates. */
4409 tree
4410 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4412 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4414 hashval_t hashcode = 0;
4415 tree ntype;
4416 enum tree_code code = TREE_CODE (ttype);
4418 /* Building a distinct copy of a tagged type is inappropriate; it
4419 causes breakage in code that expects there to be a one-to-one
4420 relationship between a struct and its fields.
4421 build_duplicate_type is another solution (as used in
4422 handle_transparent_union_attribute), but that doesn't play well
4423 with the stronger C++ type identity model. */
4424 if (TREE_CODE (ttype) == RECORD_TYPE
4425 || TREE_CODE (ttype) == UNION_TYPE
4426 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4427 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4429 warning (OPT_Wattributes,
4430 "ignoring attributes applied to %qT after definition",
4431 TYPE_MAIN_VARIANT (ttype));
4432 return build_qualified_type (ttype, quals);
4435 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4436 ntype = build_distinct_type_copy (ttype);
4438 TYPE_ATTRIBUTES (ntype) = attribute;
4440 hashcode = iterative_hash_object (code, hashcode);
4441 if (TREE_TYPE (ntype))
4442 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4443 hashcode);
4444 hashcode = attribute_hash_list (attribute, hashcode);
4446 switch (TREE_CODE (ntype))
4448 case FUNCTION_TYPE:
4449 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4450 break;
4451 case ARRAY_TYPE:
4452 if (TYPE_DOMAIN (ntype))
4453 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4454 hashcode);
4455 break;
4456 case INTEGER_TYPE:
4457 hashcode = iterative_hash_object
4458 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4459 hashcode = iterative_hash_object
4460 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4461 break;
4462 case REAL_TYPE:
4463 case FIXED_POINT_TYPE:
4465 unsigned int precision = TYPE_PRECISION (ntype);
4466 hashcode = iterative_hash_object (precision, hashcode);
4468 break;
4469 default:
4470 break;
4473 ntype = type_hash_canon (hashcode, ntype);
4475 /* If the target-dependent attributes make NTYPE different from
4476 its canonical type, we will need to use structural equality
4477 checks for this type. */
4478 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4479 || !comp_type_attributes (ntype, ttype))
4480 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4481 else if (TYPE_CANONICAL (ntype) == ntype)
4482 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4484 ttype = build_qualified_type (ntype, quals);
4486 else if (TYPE_QUALS (ttype) != quals)
4487 ttype = build_qualified_type (ttype, quals);
4489 return ttype;
4492 /* Compare two attributes for their value identity. Return true if the
4493 attribute values are known to be equal; otherwise return false.
4496 static bool
4497 attribute_value_equal (const_tree attr1, const_tree attr2)
4499 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4500 return true;
4502 if (TREE_VALUE (attr1) != NULL_TREE
4503 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4504 && TREE_VALUE (attr2) != NULL
4505 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4506 return (simple_cst_list_equal (TREE_VALUE (attr1),
4507 TREE_VALUE (attr2)) == 1);
4509 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4512 /* Return 0 if the attributes for two types are incompatible, 1 if they
4513 are compatible, and 2 if they are nearly compatible (which causes a
4514 warning to be generated). */
4516 comp_type_attributes (const_tree type1, const_tree type2)
4518 const_tree a1 = TYPE_ATTRIBUTES (type1);
4519 const_tree a2 = TYPE_ATTRIBUTES (type2);
4520 const_tree a;
4522 if (a1 == a2)
4523 return 1;
4524 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4526 const struct attribute_spec *as;
4527 const_tree attr;
4529 as = lookup_attribute_spec (get_attribute_name (a));
4530 if (!as || as->affects_type_identity == false)
4531 continue;
4533 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4534 if (!attr || !attribute_value_equal (a, attr))
4535 break;
4537 if (!a)
4539 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4541 const struct attribute_spec *as;
4543 as = lookup_attribute_spec (get_attribute_name (a));
4544 if (!as || as->affects_type_identity == false)
4545 continue;
4547 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4548 break;
4549 /* We don't need to compare trees again, as we did this
4550 already in first loop. */
4552 /* All types - affecting identity - are equal, so
4553 there is no need to call target hook for comparison. */
4554 if (!a)
4555 return 1;
4557 /* As some type combinations - like default calling-convention - might
4558 be compatible, we have to call the target hook to get the final result. */
4559 return targetm.comp_type_attributes (type1, type2);
4562 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4563 is ATTRIBUTE.
4565 Record such modified types already made so we don't make duplicates. */
4567 tree
4568 build_type_attribute_variant (tree ttype, tree attribute)
4570 return build_type_attribute_qual_variant (ttype, attribute,
4571 TYPE_QUALS (ttype));
4575 /* Reset the expression *EXPR_P, a size or position.
4577 ??? We could reset all non-constant sizes or positions. But it's cheap
4578 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4580 We need to reset self-referential sizes or positions because they cannot
4581 be gimplified and thus can contain a CALL_EXPR after the gimplification
4582 is finished, which will run afoul of LTO streaming. And they need to be
4583 reset to something essentially dummy but not constant, so as to preserve
4584 the properties of the object they are attached to. */
4586 static inline void
4587 free_lang_data_in_one_sizepos (tree *expr_p)
4589 tree expr = *expr_p;
4590 if (CONTAINS_PLACEHOLDER_P (expr))
4591 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4595 /* Reset all the fields in a binfo node BINFO. We only keep
4596 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4598 static void
4599 free_lang_data_in_binfo (tree binfo)
4601 unsigned i;
4602 tree t;
4604 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4606 BINFO_VIRTUALS (binfo) = NULL_TREE;
4607 BINFO_BASE_ACCESSES (binfo) = NULL;
4608 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4609 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4611 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4612 free_lang_data_in_binfo (t);
4616 /* Reset all language specific information still present in TYPE. */
4618 static void
4619 free_lang_data_in_type (tree type)
4621 gcc_assert (TYPE_P (type));
4623 /* Give the FE a chance to remove its own data first. */
4624 lang_hooks.free_lang_data (type);
4626 TREE_LANG_FLAG_0 (type) = 0;
4627 TREE_LANG_FLAG_1 (type) = 0;
4628 TREE_LANG_FLAG_2 (type) = 0;
4629 TREE_LANG_FLAG_3 (type) = 0;
4630 TREE_LANG_FLAG_4 (type) = 0;
4631 TREE_LANG_FLAG_5 (type) = 0;
4632 TREE_LANG_FLAG_6 (type) = 0;
4634 if (TREE_CODE (type) == FUNCTION_TYPE)
4636 /* Remove the const and volatile qualifiers from arguments. The
4637 C++ front end removes them, but the C front end does not,
4638 leading to false ODR violation errors when merging two
4639 instances of the same function signature compiled by
4640 different front ends. */
4641 tree p;
4643 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4645 tree arg_type = TREE_VALUE (p);
4647 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4649 int quals = TYPE_QUALS (arg_type)
4650 & ~TYPE_QUAL_CONST
4651 & ~TYPE_QUAL_VOLATILE;
4652 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4653 free_lang_data_in_type (TREE_VALUE (p));
4658 /* Remove members that are not actually FIELD_DECLs from the field
4659 list of an aggregate. These occur in C++. */
4660 if (RECORD_OR_UNION_TYPE_P (type))
4662 tree prev, member;
4664 /* Note that TYPE_FIELDS can be shared across distinct
4665 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4666 to be removed, we cannot set its TREE_CHAIN to NULL.
4667 Otherwise, we would not be able to find all the other fields
4668 in the other instances of this TREE_TYPE.
4670 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4671 prev = NULL_TREE;
4672 member = TYPE_FIELDS (type);
4673 while (member)
4675 if (TREE_CODE (member) == FIELD_DECL
4676 || TREE_CODE (member) == TYPE_DECL)
4678 if (prev)
4679 TREE_CHAIN (prev) = member;
4680 else
4681 TYPE_FIELDS (type) = member;
4682 prev = member;
4685 member = TREE_CHAIN (member);
4688 if (prev)
4689 TREE_CHAIN (prev) = NULL_TREE;
4690 else
4691 TYPE_FIELDS (type) = NULL_TREE;
4693 TYPE_METHODS (type) = NULL_TREE;
4694 if (TYPE_BINFO (type))
4695 free_lang_data_in_binfo (TYPE_BINFO (type));
4697 else
4699 /* For non-aggregate types, clear out the language slot (which
4700 overloads TYPE_BINFO). */
4701 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4703 if (INTEGRAL_TYPE_P (type)
4704 || SCALAR_FLOAT_TYPE_P (type)
4705 || FIXED_POINT_TYPE_P (type))
4707 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4708 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4712 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4713 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4715 if (TYPE_CONTEXT (type)
4716 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4718 tree ctx = TYPE_CONTEXT (type);
4721 ctx = BLOCK_SUPERCONTEXT (ctx);
4723 while (ctx && TREE_CODE (ctx) == BLOCK);
4724 TYPE_CONTEXT (type) = ctx;
4729 /* Return true if DECL may need an assembler name to be set. */
4731 static inline bool
4732 need_assembler_name_p (tree decl)
4734 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4735 if (TREE_CODE (decl) != FUNCTION_DECL
4736 && TREE_CODE (decl) != VAR_DECL)
4737 return false;
4739 /* If DECL already has its assembler name set, it does not need a
4740 new one. */
4741 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4742 || DECL_ASSEMBLER_NAME_SET_P (decl))
4743 return false;
4745 /* Abstract decls do not need an assembler name. */
4746 if (DECL_ABSTRACT (decl))
4747 return false;
4749 /* For VAR_DECLs, only static, public and external symbols need an
4750 assembler name. */
4751 if (TREE_CODE (decl) == VAR_DECL
4752 && !TREE_STATIC (decl)
4753 && !TREE_PUBLIC (decl)
4754 && !DECL_EXTERNAL (decl))
4755 return false;
4757 if (TREE_CODE (decl) == FUNCTION_DECL)
4759 /* Do not set assembler name on builtins. Allow RTL expansion to
4760 decide whether to expand inline or via a regular call. */
4761 if (DECL_BUILT_IN (decl)
4762 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4763 return false;
4765 /* Functions represented in the callgraph need an assembler name. */
4766 if (cgraph_get_node (decl) != NULL)
4767 return true;
4769 /* Unused and not public functions don't need an assembler name. */
4770 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4771 return false;
4774 return true;
4778 /* Reset all language specific information still present in symbol
4779 DECL. */
4781 static void
4782 free_lang_data_in_decl (tree decl)
4784 gcc_assert (DECL_P (decl));
4786 /* Give the FE a chance to remove its own data first. */
4787 lang_hooks.free_lang_data (decl);
4789 TREE_LANG_FLAG_0 (decl) = 0;
4790 TREE_LANG_FLAG_1 (decl) = 0;
4791 TREE_LANG_FLAG_2 (decl) = 0;
4792 TREE_LANG_FLAG_3 (decl) = 0;
4793 TREE_LANG_FLAG_4 (decl) = 0;
4794 TREE_LANG_FLAG_5 (decl) = 0;
4795 TREE_LANG_FLAG_6 (decl) = 0;
4797 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4798 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4799 if (TREE_CODE (decl) == FIELD_DECL)
4801 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4802 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4803 DECL_QUALIFIER (decl) = NULL_TREE;
4806 if (TREE_CODE (decl) == FUNCTION_DECL)
4808 if (gimple_has_body_p (decl))
4810 tree t;
4812 /* If DECL has a gimple body, then the context for its
4813 arguments must be DECL. Otherwise, it doesn't really
4814 matter, as we will not be emitting any code for DECL. In
4815 general, there may be other instances of DECL created by
4816 the front end and since PARM_DECLs are generally shared,
4817 their DECL_CONTEXT changes as the replicas of DECL are
4818 created. The only time where DECL_CONTEXT is important
4819 is for the FUNCTION_DECLs that have a gimple body (since
4820 the PARM_DECL will be used in the function's body). */
4821 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4822 DECL_CONTEXT (t) = decl;
4825 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
4826 At this point, it is not needed anymore. */
4827 DECL_SAVED_TREE (decl) = NULL_TREE;
4829 /* Clear the abstract origin if it refers to a method. Otherwise
4830 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
4831 origin will not be output correctly. */
4832 if (DECL_ABSTRACT_ORIGIN (decl)
4833 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
4834 && RECORD_OR_UNION_TYPE_P
4835 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
4836 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
4838 /* Sometimes the C++ frontend doesn't manage to transform a temporary
4839 DECL_VINDEX referring to itself into a vtable slot number as it
4840 should. Happens with functions that are copied and then forgotten
4841 about. Just clear it, it won't matter anymore. */
4842 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
4843 DECL_VINDEX (decl) = NULL_TREE;
4845 else if (TREE_CODE (decl) == VAR_DECL)
4847 if ((DECL_EXTERNAL (decl)
4848 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
4849 || (decl_function_context (decl) && !TREE_STATIC (decl)))
4850 DECL_INITIAL (decl) = NULL_TREE;
4852 else if (TREE_CODE (decl) == TYPE_DECL
4853 || TREE_CODE (decl) == FIELD_DECL)
4854 DECL_INITIAL (decl) = NULL_TREE;
4855 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
4856 && DECL_INITIAL (decl)
4857 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
4859 /* Strip builtins from the translation-unit BLOCK. We still have targets
4860 without builtin_decl_explicit support and also builtins are shared
4861 nodes and thus we can't use TREE_CHAIN in multiple lists. */
4862 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
4863 while (*nextp)
4865 tree var = *nextp;
4866 if (TREE_CODE (var) == FUNCTION_DECL
4867 && DECL_BUILT_IN (var))
4868 *nextp = TREE_CHAIN (var);
4869 else
4870 nextp = &TREE_CHAIN (var);
4876 /* Data used when collecting DECLs and TYPEs for language data removal. */
4878 struct free_lang_data_d
4880 /* Worklist to avoid excessive recursion. */
4881 vec<tree> worklist;
4883 /* Set of traversed objects. Used to avoid duplicate visits. */
4884 struct pointer_set_t *pset;
4886 /* Array of symbols to process with free_lang_data_in_decl. */
4887 vec<tree> decls;
4889 /* Array of types to process with free_lang_data_in_type. */
4890 vec<tree> types;
4894 /* Save all language fields needed to generate proper debug information
4895 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
4897 static void
4898 save_debug_info_for_decl (tree t)
4900 /*struct saved_debug_info_d *sdi;*/
4902 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
4904 /* FIXME. Partial implementation for saving debug info removed. */
4908 /* Save all language fields needed to generate proper debug information
4909 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
4911 static void
4912 save_debug_info_for_type (tree t)
4914 /*struct saved_debug_info_d *sdi;*/
4916 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
4918 /* FIXME. Partial implementation for saving debug info removed. */
4922 /* Add type or decl T to one of the list of tree nodes that need their
4923 language data removed. The lists are held inside FLD. */
4925 static void
4926 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
4928 if (DECL_P (t))
4930 fld->decls.safe_push (t);
4931 if (debug_info_level > DINFO_LEVEL_TERSE)
4932 save_debug_info_for_decl (t);
4934 else if (TYPE_P (t))
4936 fld->types.safe_push (t);
4937 if (debug_info_level > DINFO_LEVEL_TERSE)
4938 save_debug_info_for_type (t);
4940 else
4941 gcc_unreachable ();
4944 /* Push tree node T into FLD->WORKLIST. */
4946 static inline void
4947 fld_worklist_push (tree t, struct free_lang_data_d *fld)
4949 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
4950 fld->worklist.safe_push ((t));
4954 /* Operand callback helper for free_lang_data_in_node. *TP is the
4955 subtree operand being considered. */
4957 static tree
4958 find_decls_types_r (tree *tp, int *ws, void *data)
4960 tree t = *tp;
4961 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
4963 if (TREE_CODE (t) == TREE_LIST)
4964 return NULL_TREE;
4966 /* Language specific nodes will be removed, so there is no need
4967 to gather anything under them. */
4968 if (is_lang_specific (t))
4970 *ws = 0;
4971 return NULL_TREE;
4974 if (DECL_P (t))
4976 /* Note that walk_tree does not traverse every possible field in
4977 decls, so we have to do our own traversals here. */
4978 add_tree_to_fld_list (t, fld);
4980 fld_worklist_push (DECL_NAME (t), fld);
4981 fld_worklist_push (DECL_CONTEXT (t), fld);
4982 fld_worklist_push (DECL_SIZE (t), fld);
4983 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
4985 /* We are going to remove everything under DECL_INITIAL for
4986 TYPE_DECLs. No point walking them. */
4987 if (TREE_CODE (t) != TYPE_DECL)
4988 fld_worklist_push (DECL_INITIAL (t), fld);
4990 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
4991 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
4993 if (TREE_CODE (t) == FUNCTION_DECL)
4995 fld_worklist_push (DECL_ARGUMENTS (t), fld);
4996 fld_worklist_push (DECL_RESULT (t), fld);
4998 else if (TREE_CODE (t) == TYPE_DECL)
5000 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5001 fld_worklist_push (DECL_VINDEX (t), fld);
5002 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5004 else if (TREE_CODE (t) == FIELD_DECL)
5006 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5007 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5008 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5009 fld_worklist_push (DECL_FCONTEXT (t), fld);
5011 else if (TREE_CODE (t) == VAR_DECL)
5013 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5014 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5017 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5018 && DECL_HAS_VALUE_EXPR_P (t))
5019 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5021 if (TREE_CODE (t) != FIELD_DECL
5022 && TREE_CODE (t) != TYPE_DECL)
5023 fld_worklist_push (TREE_CHAIN (t), fld);
5024 *ws = 0;
5026 else if (TYPE_P (t))
5028 /* Note that walk_tree does not traverse every possible field in
5029 types, so we have to do our own traversals here. */
5030 add_tree_to_fld_list (t, fld);
5032 if (!RECORD_OR_UNION_TYPE_P (t))
5033 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5034 fld_worklist_push (TYPE_SIZE (t), fld);
5035 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5036 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5037 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5038 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5039 fld_worklist_push (TYPE_NAME (t), fld);
5040 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5041 them and thus do not and want not to reach unused pointer types
5042 this way. */
5043 if (!POINTER_TYPE_P (t))
5044 fld_worklist_push (TYPE_MINVAL (t), fld);
5045 if (!RECORD_OR_UNION_TYPE_P (t))
5046 fld_worklist_push (TYPE_MAXVAL (t), fld);
5047 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5048 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5049 do not and want not to reach unused variants this way. */
5050 if (TYPE_CONTEXT (t))
5052 tree ctx = TYPE_CONTEXT (t);
5053 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5054 So push that instead. */
5055 while (ctx && TREE_CODE (ctx) == BLOCK)
5056 ctx = BLOCK_SUPERCONTEXT (ctx);
5057 fld_worklist_push (ctx, fld);
5059 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5060 and want not to reach unused types this way. */
5062 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5064 unsigned i;
5065 tree tem;
5066 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5067 fld_worklist_push (TREE_TYPE (tem), fld);
5068 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5069 if (tem
5070 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5071 && TREE_CODE (tem) == TREE_LIST)
5074 fld_worklist_push (TREE_VALUE (tem), fld);
5075 tem = TREE_CHAIN (tem);
5077 while (tem);
5079 if (RECORD_OR_UNION_TYPE_P (t))
5081 tree tem;
5082 /* Push all TYPE_FIELDS - there can be interleaving interesting
5083 and non-interesting things. */
5084 tem = TYPE_FIELDS (t);
5085 while (tem)
5087 if (TREE_CODE (tem) == FIELD_DECL
5088 || TREE_CODE (tem) == TYPE_DECL)
5089 fld_worklist_push (tem, fld);
5090 tem = TREE_CHAIN (tem);
5094 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5095 *ws = 0;
5097 else if (TREE_CODE (t) == BLOCK)
5099 tree tem;
5100 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5101 fld_worklist_push (tem, fld);
5102 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5103 fld_worklist_push (tem, fld);
5104 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5107 if (TREE_CODE (t) != IDENTIFIER_NODE
5108 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5109 fld_worklist_push (TREE_TYPE (t), fld);
5111 return NULL_TREE;
5115 /* Find decls and types in T. */
5117 static void
5118 find_decls_types (tree t, struct free_lang_data_d *fld)
5120 while (1)
5122 if (!pointer_set_contains (fld->pset, t))
5123 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5124 if (fld->worklist.is_empty ())
5125 break;
5126 t = fld->worklist.pop ();
5130 /* Translate all the types in LIST with the corresponding runtime
5131 types. */
5133 static tree
5134 get_eh_types_for_runtime (tree list)
5136 tree head, prev;
5138 if (list == NULL_TREE)
5139 return NULL_TREE;
5141 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5142 prev = head;
5143 list = TREE_CHAIN (list);
5144 while (list)
5146 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5147 TREE_CHAIN (prev) = n;
5148 prev = TREE_CHAIN (prev);
5149 list = TREE_CHAIN (list);
5152 return head;
5156 /* Find decls and types referenced in EH region R and store them in
5157 FLD->DECLS and FLD->TYPES. */
5159 static void
5160 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5162 switch (r->type)
5164 case ERT_CLEANUP:
5165 break;
5167 case ERT_TRY:
5169 eh_catch c;
5171 /* The types referenced in each catch must first be changed to the
5172 EH types used at runtime. This removes references to FE types
5173 in the region. */
5174 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5176 c->type_list = get_eh_types_for_runtime (c->type_list);
5177 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5180 break;
5182 case ERT_ALLOWED_EXCEPTIONS:
5183 r->u.allowed.type_list
5184 = get_eh_types_for_runtime (r->u.allowed.type_list);
5185 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5186 break;
5188 case ERT_MUST_NOT_THROW:
5189 walk_tree (&r->u.must_not_throw.failure_decl,
5190 find_decls_types_r, fld, fld->pset);
5191 break;
5196 /* Find decls and types referenced in cgraph node N and store them in
5197 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5198 look for *every* kind of DECL and TYPE node reachable from N,
5199 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5200 NAMESPACE_DECLs, etc). */
5202 static void
5203 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5205 basic_block bb;
5206 struct function *fn;
5207 unsigned ix;
5208 tree t;
5210 find_decls_types (n->symbol.decl, fld);
5212 if (!gimple_has_body_p (n->symbol.decl))
5213 return;
5215 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5217 fn = DECL_STRUCT_FUNCTION (n->symbol.decl);
5219 /* Traverse locals. */
5220 FOR_EACH_LOCAL_DECL (fn, ix, t)
5221 find_decls_types (t, fld);
5223 /* Traverse EH regions in FN. */
5225 eh_region r;
5226 FOR_ALL_EH_REGION_FN (r, fn)
5227 find_decls_types_in_eh_region (r, fld);
5230 /* Traverse every statement in FN. */
5231 FOR_EACH_BB_FN (bb, fn)
5233 gimple_stmt_iterator si;
5234 unsigned i;
5236 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5238 gimple phi = gsi_stmt (si);
5240 for (i = 0; i < gimple_phi_num_args (phi); i++)
5242 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5243 find_decls_types (*arg_p, fld);
5247 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5249 gimple stmt = gsi_stmt (si);
5251 if (is_gimple_call (stmt))
5252 find_decls_types (gimple_call_fntype (stmt), fld);
5254 for (i = 0; i < gimple_num_ops (stmt); i++)
5256 tree arg = gimple_op (stmt, i);
5257 find_decls_types (arg, fld);
5264 /* Find decls and types referenced in varpool node N and store them in
5265 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5266 look for *every* kind of DECL and TYPE node reachable from N,
5267 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5268 NAMESPACE_DECLs, etc). */
5270 static void
5271 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5273 find_decls_types (v->symbol.decl, fld);
5276 /* If T needs an assembler name, have one created for it. */
5278 void
5279 assign_assembler_name_if_neeeded (tree t)
5281 if (need_assembler_name_p (t))
5283 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5284 diagnostics that use input_location to show locus
5285 information. The problem here is that, at this point,
5286 input_location is generally anchored to the end of the file
5287 (since the parser is long gone), so we don't have a good
5288 position to pin it to.
5290 To alleviate this problem, this uses the location of T's
5291 declaration. Examples of this are
5292 testsuite/g++.dg/template/cond2.C and
5293 testsuite/g++.dg/template/pr35240.C. */
5294 location_t saved_location = input_location;
5295 input_location = DECL_SOURCE_LOCATION (t);
5297 decl_assembler_name (t);
5299 input_location = saved_location;
5304 /* Free language specific information for every operand and expression
5305 in every node of the call graph. This process operates in three stages:
5307 1- Every callgraph node and varpool node is traversed looking for
5308 decls and types embedded in them. This is a more exhaustive
5309 search than that done by find_referenced_vars, because it will
5310 also collect individual fields, decls embedded in types, etc.
5312 2- All the decls found are sent to free_lang_data_in_decl.
5314 3- All the types found are sent to free_lang_data_in_type.
5316 The ordering between decls and types is important because
5317 free_lang_data_in_decl sets assembler names, which includes
5318 mangling. So types cannot be freed up until assembler names have
5319 been set up. */
5321 static void
5322 free_lang_data_in_cgraph (void)
5324 struct cgraph_node *n;
5325 struct varpool_node *v;
5326 struct free_lang_data_d fld;
5327 tree t;
5328 unsigned i;
5329 alias_pair *p;
5331 /* Initialize sets and arrays to store referenced decls and types. */
5332 fld.pset = pointer_set_create ();
5333 fld.worklist.create (0);
5334 fld.decls.create (100);
5335 fld.types.create (100);
5337 /* Find decls and types in the body of every function in the callgraph. */
5338 FOR_EACH_FUNCTION (n)
5339 find_decls_types_in_node (n, &fld);
5341 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5342 find_decls_types (p->decl, &fld);
5344 /* Find decls and types in every varpool symbol. */
5345 FOR_EACH_VARIABLE (v)
5346 find_decls_types_in_var (v, &fld);
5348 /* Set the assembler name on every decl found. We need to do this
5349 now because free_lang_data_in_decl will invalidate data needed
5350 for mangling. This breaks mangling on interdependent decls. */
5351 FOR_EACH_VEC_ELT (fld.decls, i, t)
5352 assign_assembler_name_if_neeeded (t);
5354 /* Traverse every decl found freeing its language data. */
5355 FOR_EACH_VEC_ELT (fld.decls, i, t)
5356 free_lang_data_in_decl (t);
5358 /* Traverse every type found freeing its language data. */
5359 FOR_EACH_VEC_ELT (fld.types, i, t)
5360 free_lang_data_in_type (t);
5362 pointer_set_destroy (fld.pset);
5363 fld.worklist.release ();
5364 fld.decls.release ();
5365 fld.types.release ();
5369 /* Free resources that are used by FE but are not needed once they are done. */
5371 static unsigned
5372 free_lang_data (void)
5374 unsigned i;
5376 /* If we are the LTO frontend we have freed lang-specific data already. */
5377 if (in_lto_p
5378 || !flag_generate_lto)
5379 return 0;
5381 /* Allocate and assign alias sets to the standard integer types
5382 while the slots are still in the way the frontends generated them. */
5383 for (i = 0; i < itk_none; ++i)
5384 if (integer_types[i])
5385 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5387 /* Traverse the IL resetting language specific information for
5388 operands, expressions, etc. */
5389 free_lang_data_in_cgraph ();
5391 /* Create gimple variants for common types. */
5392 ptrdiff_type_node = integer_type_node;
5393 fileptr_type_node = ptr_type_node;
5395 /* Reset some langhooks. Do not reset types_compatible_p, it may
5396 still be used indirectly via the get_alias_set langhook. */
5397 lang_hooks.dwarf_name = lhd_dwarf_name;
5398 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5399 /* We do not want the default decl_assembler_name implementation,
5400 rather if we have fixed everything we want a wrapper around it
5401 asserting that all non-local symbols already got their assembler
5402 name and only produce assembler names for local symbols. Or rather
5403 make sure we never call decl_assembler_name on local symbols and
5404 devise a separate, middle-end private scheme for it. */
5406 /* Reset diagnostic machinery. */
5407 tree_diagnostics_defaults (global_dc);
5409 return 0;
5413 struct simple_ipa_opt_pass pass_ipa_free_lang_data =
5416 SIMPLE_IPA_PASS,
5417 "*free_lang_data", /* name */
5418 OPTGROUP_NONE, /* optinfo_flags */
5419 NULL, /* gate */
5420 free_lang_data, /* execute */
5421 NULL, /* sub */
5422 NULL, /* next */
5423 0, /* static_pass_number */
5424 TV_IPA_FREE_LANG_DATA, /* tv_id */
5425 0, /* properties_required */
5426 0, /* properties_provided */
5427 0, /* properties_destroyed */
5428 0, /* todo_flags_start */
5429 0 /* todo_flags_finish */
5433 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5434 ATTR_NAME. Also used internally by remove_attribute(). */
5435 bool
5436 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5438 size_t ident_len = IDENTIFIER_LENGTH (ident);
5440 if (ident_len == attr_len)
5442 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5443 return true;
5445 else if (ident_len == attr_len + 4)
5447 /* There is the possibility that ATTR is 'text' and IDENT is
5448 '__text__'. */
5449 const char *p = IDENTIFIER_POINTER (ident);
5450 if (p[0] == '_' && p[1] == '_'
5451 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5452 && strncmp (attr_name, p + 2, attr_len) == 0)
5453 return true;
5456 return false;
5459 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5460 of ATTR_NAME, and LIST is not NULL_TREE. */
5461 tree
5462 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5464 while (list)
5466 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5468 if (ident_len == attr_len)
5470 if (!strcmp (attr_name,
5471 IDENTIFIER_POINTER (get_attribute_name (list))))
5472 break;
5474 /* TODO: If we made sure that attributes were stored in the
5475 canonical form without '__...__' (ie, as in 'text' as opposed
5476 to '__text__') then we could avoid the following case. */
5477 else if (ident_len == attr_len + 4)
5479 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5480 if (p[0] == '_' && p[1] == '_'
5481 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5482 && strncmp (attr_name, p + 2, attr_len) == 0)
5483 break;
5485 list = TREE_CHAIN (list);
5488 return list;
5491 /* A variant of lookup_attribute() that can be used with an identifier
5492 as the first argument, and where the identifier can be either
5493 'text' or '__text__'.
5495 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5496 return a pointer to the attribute's list element if the attribute
5497 is part of the list, or NULL_TREE if not found. If the attribute
5498 appears more than once, this only returns the first occurrence; the
5499 TREE_CHAIN of the return value should be passed back in if further
5500 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5501 can be in the form 'text' or '__text__'. */
5502 static tree
5503 lookup_ident_attribute (tree attr_identifier, tree list)
5505 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5507 while (list)
5509 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5510 == IDENTIFIER_NODE);
5512 /* Identifiers can be compared directly for equality. */
5513 if (attr_identifier == get_attribute_name (list))
5514 break;
5516 /* If they are not equal, they may still be one in the form
5517 'text' while the other one is in the form '__text__'. TODO:
5518 If we were storing attributes in normalized 'text' form, then
5519 this could all go away and we could take full advantage of
5520 the fact that we're comparing identifiers. :-) */
5522 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5523 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5525 if (ident_len == attr_len + 4)
5527 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5528 const char *q = IDENTIFIER_POINTER (attr_identifier);
5529 if (p[0] == '_' && p[1] == '_'
5530 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5531 && strncmp (q, p + 2, attr_len) == 0)
5532 break;
5534 else if (ident_len + 4 == attr_len)
5536 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5537 const char *q = IDENTIFIER_POINTER (attr_identifier);
5538 if (q[0] == '_' && q[1] == '_'
5539 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5540 && strncmp (q + 2, p, ident_len) == 0)
5541 break;
5544 list = TREE_CHAIN (list);
5547 return list;
5550 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5551 modified list. */
5553 tree
5554 remove_attribute (const char *attr_name, tree list)
5556 tree *p;
5557 size_t attr_len = strlen (attr_name);
5559 gcc_checking_assert (attr_name[0] != '_');
5561 for (p = &list; *p; )
5563 tree l = *p;
5564 /* TODO: If we were storing attributes in normalized form, here
5565 we could use a simple strcmp(). */
5566 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5567 *p = TREE_CHAIN (l);
5568 else
5569 p = &TREE_CHAIN (l);
5572 return list;
5575 /* Return an attribute list that is the union of a1 and a2. */
5577 tree
5578 merge_attributes (tree a1, tree a2)
5580 tree attributes;
5582 /* Either one unset? Take the set one. */
5584 if ((attributes = a1) == 0)
5585 attributes = a2;
5587 /* One that completely contains the other? Take it. */
5589 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5591 if (attribute_list_contained (a2, a1))
5592 attributes = a2;
5593 else
5595 /* Pick the longest list, and hang on the other list. */
5597 if (list_length (a1) < list_length (a2))
5598 attributes = a2, a2 = a1;
5600 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5602 tree a;
5603 for (a = lookup_ident_attribute (get_attribute_name (a2),
5604 attributes);
5605 a != NULL_TREE && !attribute_value_equal (a, a2);
5606 a = lookup_ident_attribute (get_attribute_name (a2),
5607 TREE_CHAIN (a)))
5609 if (a == NULL_TREE)
5611 a1 = copy_node (a2);
5612 TREE_CHAIN (a1) = attributes;
5613 attributes = a1;
5618 return attributes;
5621 /* Given types T1 and T2, merge their attributes and return
5622 the result. */
5624 tree
5625 merge_type_attributes (tree t1, tree t2)
5627 return merge_attributes (TYPE_ATTRIBUTES (t1),
5628 TYPE_ATTRIBUTES (t2));
5631 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5632 the result. */
5634 tree
5635 merge_decl_attributes (tree olddecl, tree newdecl)
5637 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5638 DECL_ATTRIBUTES (newdecl));
5641 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5643 /* Specialization of merge_decl_attributes for various Windows targets.
5645 This handles the following situation:
5647 __declspec (dllimport) int foo;
5648 int foo;
5650 The second instance of `foo' nullifies the dllimport. */
5652 tree
5653 merge_dllimport_decl_attributes (tree old, tree new_tree)
5655 tree a;
5656 int delete_dllimport_p = 1;
5658 /* What we need to do here is remove from `old' dllimport if it doesn't
5659 appear in `new'. dllimport behaves like extern: if a declaration is
5660 marked dllimport and a definition appears later, then the object
5661 is not dllimport'd. We also remove a `new' dllimport if the old list
5662 contains dllexport: dllexport always overrides dllimport, regardless
5663 of the order of declaration. */
5664 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5665 delete_dllimport_p = 0;
5666 else if (DECL_DLLIMPORT_P (new_tree)
5667 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5669 DECL_DLLIMPORT_P (new_tree) = 0;
5670 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5671 "dllimport ignored", new_tree);
5673 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5675 /* Warn about overriding a symbol that has already been used, e.g.:
5676 extern int __attribute__ ((dllimport)) foo;
5677 int* bar () {return &foo;}
5678 int foo;
5680 if (TREE_USED (old))
5682 warning (0, "%q+D redeclared without dllimport attribute "
5683 "after being referenced with dll linkage", new_tree);
5684 /* If we have used a variable's address with dllimport linkage,
5685 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5686 decl may already have had TREE_CONSTANT computed.
5687 We still remove the attribute so that assembler code refers
5688 to '&foo rather than '_imp__foo'. */
5689 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5690 DECL_DLLIMPORT_P (new_tree) = 1;
5693 /* Let an inline definition silently override the external reference,
5694 but otherwise warn about attribute inconsistency. */
5695 else if (TREE_CODE (new_tree) == VAR_DECL
5696 || !DECL_DECLARED_INLINE_P (new_tree))
5697 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5698 "previous dllimport ignored", new_tree);
5700 else
5701 delete_dllimport_p = 0;
5703 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5705 if (delete_dllimport_p)
5706 a = remove_attribute ("dllimport", a);
5708 return a;
5711 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5712 struct attribute_spec.handler. */
5714 tree
5715 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5716 bool *no_add_attrs)
5718 tree node = *pnode;
5719 bool is_dllimport;
5721 /* These attributes may apply to structure and union types being created,
5722 but otherwise should pass to the declaration involved. */
5723 if (!DECL_P (node))
5725 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5726 | (int) ATTR_FLAG_ARRAY_NEXT))
5728 *no_add_attrs = true;
5729 return tree_cons (name, args, NULL_TREE);
5731 if (TREE_CODE (node) == RECORD_TYPE
5732 || TREE_CODE (node) == UNION_TYPE)
5734 node = TYPE_NAME (node);
5735 if (!node)
5736 return NULL_TREE;
5738 else
5740 warning (OPT_Wattributes, "%qE attribute ignored",
5741 name);
5742 *no_add_attrs = true;
5743 return NULL_TREE;
5747 if (TREE_CODE (node) != FUNCTION_DECL
5748 && TREE_CODE (node) != VAR_DECL
5749 && TREE_CODE (node) != TYPE_DECL)
5751 *no_add_attrs = true;
5752 warning (OPT_Wattributes, "%qE attribute ignored",
5753 name);
5754 return NULL_TREE;
5757 if (TREE_CODE (node) == TYPE_DECL
5758 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5759 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5761 *no_add_attrs = true;
5762 warning (OPT_Wattributes, "%qE attribute ignored",
5763 name);
5764 return NULL_TREE;
5767 is_dllimport = is_attribute_p ("dllimport", name);
5769 /* Report error on dllimport ambiguities seen now before they cause
5770 any damage. */
5771 if (is_dllimport)
5773 /* Honor any target-specific overrides. */
5774 if (!targetm.valid_dllimport_attribute_p (node))
5775 *no_add_attrs = true;
5777 else if (TREE_CODE (node) == FUNCTION_DECL
5778 && DECL_DECLARED_INLINE_P (node))
5780 warning (OPT_Wattributes, "inline function %q+D declared as "
5781 " dllimport: attribute ignored", node);
5782 *no_add_attrs = true;
5784 /* Like MS, treat definition of dllimported variables and
5785 non-inlined functions on declaration as syntax errors. */
5786 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
5788 error ("function %q+D definition is marked dllimport", node);
5789 *no_add_attrs = true;
5792 else if (TREE_CODE (node) == VAR_DECL)
5794 if (DECL_INITIAL (node))
5796 error ("variable %q+D definition is marked dllimport",
5797 node);
5798 *no_add_attrs = true;
5801 /* `extern' needn't be specified with dllimport.
5802 Specify `extern' now and hope for the best. Sigh. */
5803 DECL_EXTERNAL (node) = 1;
5804 /* Also, implicitly give dllimport'd variables declared within
5805 a function global scope, unless declared static. */
5806 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
5807 TREE_PUBLIC (node) = 1;
5810 if (*no_add_attrs == false)
5811 DECL_DLLIMPORT_P (node) = 1;
5813 else if (TREE_CODE (node) == FUNCTION_DECL
5814 && DECL_DECLARED_INLINE_P (node)
5815 && flag_keep_inline_dllexport)
5816 /* An exported function, even if inline, must be emitted. */
5817 DECL_EXTERNAL (node) = 0;
5819 /* Report error if symbol is not accessible at global scope. */
5820 if (!TREE_PUBLIC (node)
5821 && (TREE_CODE (node) == VAR_DECL
5822 || TREE_CODE (node) == FUNCTION_DECL))
5824 error ("external linkage required for symbol %q+D because of "
5825 "%qE attribute", node, name);
5826 *no_add_attrs = true;
5829 /* A dllexport'd entity must have default visibility so that other
5830 program units (shared libraries or the main executable) can see
5831 it. A dllimport'd entity must have default visibility so that
5832 the linker knows that undefined references within this program
5833 unit can be resolved by the dynamic linker. */
5834 if (!*no_add_attrs)
5836 if (DECL_VISIBILITY_SPECIFIED (node)
5837 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
5838 error ("%qE implies default visibility, but %qD has already "
5839 "been declared with a different visibility",
5840 name, node);
5841 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
5842 DECL_VISIBILITY_SPECIFIED (node) = 1;
5845 return NULL_TREE;
5848 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
5850 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
5851 of the various TYPE_QUAL values. Also, set the UPC layout qualifier,
5852 which is either null or a reference to an integral constant. */
5854 static void
5855 set_type_quals (tree type, int type_quals, tree layout_qualifier)
5857 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
5858 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
5859 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
5860 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
5861 TYPE_SHARED (type) = (type_quals & TYPE_QUAL_SHARED) != 0;
5862 TYPE_STRICT (type) = (type_quals & TYPE_QUAL_STRICT) != 0;
5863 TYPE_RELAXED (type) = (type_quals & TYPE_QUAL_RELAXED) != 0;
5864 if (TYPE_SHARED (type))
5865 SET_TYPE_BLOCK_FACTOR (type, layout_qualifier);
5868 /* Returns true iff CAND is equivalent to BASE with
5869 TYPE_QUALS and LAYOUT_QUALIFIER. */
5871 bool
5872 check_qualified_type (const_tree cand, const_tree base,
5873 int type_quals, tree layout_qualifier)
5875 return (TYPE_QUALS (cand) == type_quals
5876 && (TYPE_BLOCK_FACTOR (cand) == layout_qualifier
5877 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
5878 layout_qualifier))
5879 && TYPE_NAME (cand) == TYPE_NAME (base)
5880 /* Apparently this is needed for Objective-C. */
5881 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5882 /* Check alignment. */
5883 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
5884 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5885 TYPE_ATTRIBUTES (base)));
5888 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
5890 static bool
5891 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
5893 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
5894 && (TYPE_BLOCK_FACTOR (cand) == TYPE_BLOCK_FACTOR (base)
5895 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
5896 TYPE_BLOCK_FACTOR (base)))
5897 && TYPE_NAME (cand) == TYPE_NAME (base)
5898 /* Apparently this is needed for Objective-C. */
5899 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
5900 /* Check alignment. */
5901 && TYPE_ALIGN (cand) == align
5902 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
5903 TYPE_ATTRIBUTES (base)));
5906 /* Return a version of the TYPE, qualified as indicated by the
5907 TYPE_QUALS, if one exists. If no qualified version exists yet,
5908 return NULL_TREE. */
5910 tree
5911 get_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
5913 tree t;
5915 if (TYPE_QUALS (type) == type_quals)
5916 return type;
5918 /* Search the chain of variants to see if there is already one there just
5919 like the one we need to have. If so, use that existing one. We must
5920 preserve the TYPE_NAME, since there is code that depends on this. */
5921 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
5922 if (check_qualified_type (t, type, type_quals, layout_qualifier))
5923 return t;
5925 return NULL_TREE;
5928 /* Like get_qualified_type_1, but creates the type if it does not
5929 exist. This function never returns NULL_TREE. */
5931 tree
5932 build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
5934 tree t;
5936 /* See if we already have the appropriate qualified variant. */
5937 t = get_qualified_type_1 (type, type_quals, layout_qualifier);
5939 /* If not, build it. */
5940 if (!t)
5942 t = build_variant_type_copy (type);
5943 set_type_quals (t, type_quals, layout_qualifier);
5945 if (TYPE_STRUCTURAL_EQUALITY_P (type))
5946 /* Propagate structural equality. */
5947 SET_TYPE_STRUCTURAL_EQUALITY (t);
5948 else if (TYPE_CANONICAL (type) != type)
5949 /* Build the underlying canonical type, since it is different
5950 from TYPE. */
5951 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
5952 type_quals);
5953 else
5954 /* T is its own canonical type. */
5955 TYPE_CANONICAL (t) = t;
5959 return t;
5962 /* Return a variant of TYPE, where all UPC qualifiers
5963 have been removed. */
5965 tree
5966 build_upc_unshared_type (tree type)
5968 tree u_type = type;
5969 if (TREE_CODE (type) == ARRAY_TYPE)
5971 const tree elem_type = TREE_TYPE(type);
5972 const tree u_elem_type = build_upc_unshared_type (elem_type);
5973 if (u_elem_type != elem_type)
5975 for (u_type = TYPE_MAIN_VARIANT (type);
5976 u_type && TREE_TYPE(u_type) != u_elem_type;
5977 u_type = TYPE_NEXT_VARIANT (u_type)) /* loop */;
5978 if (!u_type)
5980 u_type = build_variant_type_copy (type);
5981 TREE_TYPE (u_type) = u_elem_type;
5985 else
5987 const int quals = TYPE_QUALS (type);
5988 const int u_quals = quals & ~(TYPE_QUAL_SHARED
5989 | TYPE_QUAL_RELAXED
5990 | TYPE_QUAL_STRICT);
5991 u_type = build_qualified_type (type, u_quals);
5993 return u_type;
5996 /* Create a variant of type T with alignment ALIGN. */
5998 tree
5999 build_aligned_type (tree type, unsigned int align)
6001 tree t;
6003 if (TYPE_PACKED (type)
6004 || TYPE_ALIGN (type) == align)
6005 return type;
6007 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6008 if (check_aligned_type (t, type, align))
6009 return t;
6011 t = build_variant_type_copy (type);
6012 TYPE_ALIGN (t) = align;
6014 return t;
6017 /* Create a new distinct copy of TYPE. The new type is made its own
6018 MAIN_VARIANT. If TYPE requires structural equality checks, the
6019 resulting type requires structural equality checks; otherwise, its
6020 TYPE_CANONICAL points to itself. */
6022 tree
6023 build_distinct_type_copy (tree type)
6025 tree t = copy_node (type);
6027 TYPE_POINTER_TO (t) = 0;
6028 TYPE_REFERENCE_TO (t) = 0;
6030 /* Set the canonical type either to a new equivalence class, or
6031 propagate the need for structural equality checks. */
6032 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6033 SET_TYPE_STRUCTURAL_EQUALITY (t);
6034 else
6035 TYPE_CANONICAL (t) = t;
6037 /* Make it its own variant. */
6038 TYPE_MAIN_VARIANT (t) = t;
6039 TYPE_NEXT_VARIANT (t) = 0;
6041 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6042 whose TREE_TYPE is not t. This can also happen in the Ada
6043 frontend when using subtypes. */
6045 return t;
6048 /* Create a new variant of TYPE, equivalent but distinct. This is so
6049 the caller can modify it. TYPE_CANONICAL for the return type will
6050 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6051 are considered equal by the language itself (or that both types
6052 require structural equality checks). */
6054 tree
6055 build_variant_type_copy (tree type)
6057 tree t, m = TYPE_MAIN_VARIANT (type);
6059 t = build_distinct_type_copy (type);
6061 /* Since we're building a variant, assume that it is a non-semantic
6062 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6063 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6065 /* Add the new type to the chain of variants of TYPE. */
6066 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6067 TYPE_NEXT_VARIANT (m) = t;
6068 TYPE_MAIN_VARIANT (t) = m;
6070 return t;
6073 /* Return true if the from tree in both tree maps are equal. */
6076 tree_map_base_eq (const void *va, const void *vb)
6078 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6079 *const b = (const struct tree_map_base *) vb;
6080 return (a->from == b->from);
6083 /* Hash a from tree in a tree_base_map. */
6085 unsigned int
6086 tree_map_base_hash (const void *item)
6088 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6091 /* Return true if this tree map structure is marked for garbage collection
6092 purposes. We simply return true if the from tree is marked, so that this
6093 structure goes away when the from tree goes away. */
6096 tree_map_base_marked_p (const void *p)
6098 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6101 /* Hash a from tree in a tree_map. */
6103 unsigned int
6104 tree_map_hash (const void *item)
6106 return (((const struct tree_map *) item)->hash);
6109 /* Hash a from tree in a tree_decl_map. */
6111 unsigned int
6112 tree_decl_map_hash (const void *item)
6114 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6117 /* Return the initialization priority for DECL. */
6119 priority_type
6120 decl_init_priority_lookup (tree decl)
6122 struct tree_priority_map *h;
6123 struct tree_map_base in;
6125 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6126 in.from = decl;
6127 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6128 return h ? h->init : DEFAULT_INIT_PRIORITY;
6131 /* Return the finalization priority for DECL. */
6133 priority_type
6134 decl_fini_priority_lookup (tree decl)
6136 struct tree_priority_map *h;
6137 struct tree_map_base in;
6139 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6140 in.from = decl;
6141 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6142 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6145 /* Return the initialization and finalization priority information for
6146 DECL. If there is no previous priority information, a freshly
6147 allocated structure is returned. */
6149 static struct tree_priority_map *
6150 decl_priority_info (tree decl)
6152 struct tree_priority_map in;
6153 struct tree_priority_map *h;
6154 void **loc;
6156 in.base.from = decl;
6157 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6158 h = (struct tree_priority_map *) *loc;
6159 if (!h)
6161 h = ggc_alloc_cleared_tree_priority_map ();
6162 *loc = h;
6163 h->base.from = decl;
6164 h->init = DEFAULT_INIT_PRIORITY;
6165 h->fini = DEFAULT_INIT_PRIORITY;
6168 return h;
6171 /* Set the initialization priority for DECL to PRIORITY. */
6173 void
6174 decl_init_priority_insert (tree decl, priority_type priority)
6176 struct tree_priority_map *h;
6178 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6179 if (priority == DEFAULT_INIT_PRIORITY)
6180 return;
6181 h = decl_priority_info (decl);
6182 h->init = priority;
6185 /* Set the finalization priority for DECL to PRIORITY. */
6187 void
6188 decl_fini_priority_insert (tree decl, priority_type priority)
6190 struct tree_priority_map *h;
6192 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6193 if (priority == DEFAULT_INIT_PRIORITY)
6194 return;
6195 h = decl_priority_info (decl);
6196 h->fini = priority;
6199 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6201 static void
6202 print_debug_expr_statistics (void)
6204 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6205 (long) htab_size (debug_expr_for_decl),
6206 (long) htab_elements (debug_expr_for_decl),
6207 htab_collisions (debug_expr_for_decl));
6210 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6212 static void
6213 print_value_expr_statistics (void)
6215 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6216 (long) htab_size (value_expr_for_decl),
6217 (long) htab_elements (value_expr_for_decl),
6218 htab_collisions (value_expr_for_decl));
6221 /* Lookup a debug expression for FROM, and return it if we find one. */
6223 tree
6224 decl_debug_expr_lookup (tree from)
6226 struct tree_decl_map *h, in;
6227 in.base.from = from;
6229 h = (struct tree_decl_map *)
6230 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6231 if (h)
6232 return h->to;
6233 return NULL_TREE;
6236 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6238 void
6239 decl_debug_expr_insert (tree from, tree to)
6241 struct tree_decl_map *h;
6242 void **loc;
6244 h = ggc_alloc_tree_decl_map ();
6245 h->base.from = from;
6246 h->to = to;
6247 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6248 INSERT);
6249 *(struct tree_decl_map **) loc = h;
6252 /* Lookup a value expression for FROM, and return it if we find one. */
6254 tree
6255 decl_value_expr_lookup (tree from)
6257 struct tree_decl_map *h, in;
6258 in.base.from = from;
6260 h = (struct tree_decl_map *)
6261 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6262 if (h)
6263 return h->to;
6264 return NULL_TREE;
6267 /* Insert a mapping FROM->TO in the value expression hashtable. */
6269 void
6270 decl_value_expr_insert (tree from, tree to)
6272 struct tree_decl_map *h;
6273 void **loc;
6275 h = ggc_alloc_tree_decl_map ();
6276 h->base.from = from;
6277 h->to = to;
6278 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6279 INSERT);
6280 *(struct tree_decl_map **) loc = h;
6283 /* Lookup a vector of debug arguments for FROM, and return it if we
6284 find one. */
6286 vec<tree, va_gc> **
6287 decl_debug_args_lookup (tree from)
6289 struct tree_vec_map *h, in;
6291 if (!DECL_HAS_DEBUG_ARGS_P (from))
6292 return NULL;
6293 gcc_checking_assert (debug_args_for_decl != NULL);
6294 in.base.from = from;
6295 h = (struct tree_vec_map *)
6296 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6297 if (h)
6298 return &h->to;
6299 return NULL;
6302 /* Insert a mapping FROM->empty vector of debug arguments in the value
6303 expression hashtable. */
6305 vec<tree, va_gc> **
6306 decl_debug_args_insert (tree from)
6308 struct tree_vec_map *h;
6309 void **loc;
6311 if (DECL_HAS_DEBUG_ARGS_P (from))
6312 return decl_debug_args_lookup (from);
6313 if (debug_args_for_decl == NULL)
6314 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6315 tree_vec_map_eq, 0);
6316 h = ggc_alloc_tree_vec_map ();
6317 h->base.from = from;
6318 h->to = NULL;
6319 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6320 INSERT);
6321 *(struct tree_vec_map **) loc = h;
6322 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6323 return &h->to;
6326 /* Hashing of types so that we don't make duplicates.
6327 The entry point is `type_hash_canon'. */
6329 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6330 with types in the TREE_VALUE slots), by adding the hash codes
6331 of the individual types. */
6333 static unsigned int
6334 type_hash_list (const_tree list, hashval_t hashcode)
6336 const_tree tail;
6338 for (tail = list; tail; tail = TREE_CHAIN (tail))
6339 if (TREE_VALUE (tail) != error_mark_node)
6340 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6341 hashcode);
6343 return hashcode;
6346 /* These are the Hashtable callback functions. */
6348 /* Returns true iff the types are equivalent. */
6350 static int
6351 type_hash_eq (const void *va, const void *vb)
6353 const struct type_hash *const a = (const struct type_hash *) va,
6354 *const b = (const struct type_hash *) vb;
6356 /* First test the things that are the same for all types. */
6357 if (a->hash != b->hash
6358 || TREE_CODE (a->type) != TREE_CODE (b->type)
6359 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6360 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6361 TYPE_ATTRIBUTES (b->type))
6362 || (TREE_CODE (a->type) != COMPLEX_TYPE
6363 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6364 return 0;
6366 /* Be careful about comparing arrays before and after the element type
6367 has been completed; don't compare TYPE_ALIGN unless both types are
6368 complete. */
6369 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6370 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6371 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6372 return 0;
6374 switch (TREE_CODE (a->type))
6376 case VOID_TYPE:
6377 case COMPLEX_TYPE:
6378 case POINTER_TYPE:
6379 case REFERENCE_TYPE:
6380 case NULLPTR_TYPE:
6381 return 1;
6383 case VECTOR_TYPE:
6384 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6386 case ENUMERAL_TYPE:
6387 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6388 && !(TYPE_VALUES (a->type)
6389 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6390 && TYPE_VALUES (b->type)
6391 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6392 && type_list_equal (TYPE_VALUES (a->type),
6393 TYPE_VALUES (b->type))))
6394 return 0;
6396 /* ... fall through ... */
6398 case INTEGER_TYPE:
6399 case REAL_TYPE:
6400 case BOOLEAN_TYPE:
6401 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6402 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6403 TYPE_MAX_VALUE (b->type)))
6404 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6405 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6406 TYPE_MIN_VALUE (b->type))));
6408 case FIXED_POINT_TYPE:
6409 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6411 case OFFSET_TYPE:
6412 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6414 case METHOD_TYPE:
6415 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6416 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6417 || (TYPE_ARG_TYPES (a->type)
6418 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6419 && TYPE_ARG_TYPES (b->type)
6420 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6421 && type_list_equal (TYPE_ARG_TYPES (a->type),
6422 TYPE_ARG_TYPES (b->type)))))
6423 break;
6424 return 0;
6425 case ARRAY_TYPE:
6426 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6428 case RECORD_TYPE:
6429 case UNION_TYPE:
6430 case QUAL_UNION_TYPE:
6431 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6432 || (TYPE_FIELDS (a->type)
6433 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6434 && TYPE_FIELDS (b->type)
6435 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6436 && type_list_equal (TYPE_FIELDS (a->type),
6437 TYPE_FIELDS (b->type))));
6439 case FUNCTION_TYPE:
6440 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6441 || (TYPE_ARG_TYPES (a->type)
6442 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6443 && TYPE_ARG_TYPES (b->type)
6444 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6445 && type_list_equal (TYPE_ARG_TYPES (a->type),
6446 TYPE_ARG_TYPES (b->type))))
6447 break;
6448 return 0;
6450 default:
6451 return 0;
6454 if (lang_hooks.types.type_hash_eq != NULL)
6455 return lang_hooks.types.type_hash_eq (a->type, b->type);
6457 return 1;
6460 /* Return the cached hash value. */
6462 static hashval_t
6463 type_hash_hash (const void *item)
6465 return ((const struct type_hash *) item)->hash;
6468 /* Look in the type hash table for a type isomorphic to TYPE.
6469 If one is found, return it. Otherwise return 0. */
6471 static tree
6472 type_hash_lookup (hashval_t hashcode, tree type)
6474 struct type_hash *h, in;
6476 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6477 must call that routine before comparing TYPE_ALIGNs. */
6478 layout_type (type);
6480 in.hash = hashcode;
6481 in.type = type;
6483 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6484 hashcode);
6485 if (h)
6486 return h->type;
6487 return NULL_TREE;
6490 /* Add an entry to the type-hash-table
6491 for a type TYPE whose hash code is HASHCODE. */
6493 static void
6494 type_hash_add (hashval_t hashcode, tree type)
6496 struct type_hash *h;
6497 void **loc;
6499 h = ggc_alloc_type_hash ();
6500 h->hash = hashcode;
6501 h->type = type;
6502 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6503 *loc = (void *)h;
6506 /* Given TYPE, and HASHCODE its hash code, return the canonical
6507 object for an identical type if one already exists.
6508 Otherwise, return TYPE, and record it as the canonical object.
6510 To use this function, first create a type of the sort you want.
6511 Then compute its hash code from the fields of the type that
6512 make it different from other similar types.
6513 Then call this function and use the value. */
6515 tree
6516 type_hash_canon (unsigned int hashcode, tree type)
6518 tree t1;
6520 /* The hash table only contains main variants, so ensure that's what we're
6521 being passed. */
6522 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6524 /* See if the type is in the hash table already. If so, return it.
6525 Otherwise, add the type. */
6526 t1 = type_hash_lookup (hashcode, type);
6527 if (t1 != 0)
6529 if (GATHER_STATISTICS)
6531 tree_code_counts[(int) TREE_CODE (type)]--;
6532 tree_node_counts[(int) t_kind]--;
6533 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6535 return t1;
6537 else
6539 type_hash_add (hashcode, type);
6540 return type;
6544 /* See if the data pointed to by the type hash table is marked. We consider
6545 it marked if the type is marked or if a debug type number or symbol
6546 table entry has been made for the type. */
6548 static int
6549 type_hash_marked_p (const void *p)
6551 const_tree const type = ((const struct type_hash *) p)->type;
6553 return ggc_marked_p (type);
6556 static void
6557 print_type_hash_statistics (void)
6559 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6560 (long) htab_size (type_hash_table),
6561 (long) htab_elements (type_hash_table),
6562 htab_collisions (type_hash_table));
6565 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6566 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6567 by adding the hash codes of the individual attributes. */
6569 static unsigned int
6570 attribute_hash_list (const_tree list, hashval_t hashcode)
6572 const_tree tail;
6574 for (tail = list; tail; tail = TREE_CHAIN (tail))
6575 /* ??? Do we want to add in TREE_VALUE too? */
6576 hashcode = iterative_hash_object
6577 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6578 return hashcode;
6581 /* Given two lists of attributes, return true if list l2 is
6582 equivalent to l1. */
6585 attribute_list_equal (const_tree l1, const_tree l2)
6587 if (l1 == l2)
6588 return 1;
6590 return attribute_list_contained (l1, l2)
6591 && attribute_list_contained (l2, l1);
6594 /* Given two lists of attributes, return true if list L2 is
6595 completely contained within L1. */
6596 /* ??? This would be faster if attribute names were stored in a canonicalized
6597 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6598 must be used to show these elements are equivalent (which they are). */
6599 /* ??? It's not clear that attributes with arguments will always be handled
6600 correctly. */
6603 attribute_list_contained (const_tree l1, const_tree l2)
6605 const_tree t1, t2;
6607 /* First check the obvious, maybe the lists are identical. */
6608 if (l1 == l2)
6609 return 1;
6611 /* Maybe the lists are similar. */
6612 for (t1 = l1, t2 = l2;
6613 t1 != 0 && t2 != 0
6614 && get_attribute_name (t1) == get_attribute_name (t2)
6615 && TREE_VALUE (t1) == TREE_VALUE (t2);
6616 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6619 /* Maybe the lists are equal. */
6620 if (t1 == 0 && t2 == 0)
6621 return 1;
6623 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6625 const_tree attr;
6626 /* This CONST_CAST is okay because lookup_attribute does not
6627 modify its argument and the return value is assigned to a
6628 const_tree. */
6629 for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
6630 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6631 attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
6634 if (attr == NULL_TREE)
6635 return 0;
6638 return 1;
6641 /* Given two lists of types
6642 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6643 return 1 if the lists contain the same types in the same order.
6644 Also, the TREE_PURPOSEs must match. */
6647 type_list_equal (const_tree l1, const_tree l2)
6649 const_tree t1, t2;
6651 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6652 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6653 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6654 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6655 && (TREE_TYPE (TREE_PURPOSE (t1))
6656 == TREE_TYPE (TREE_PURPOSE (t2))))))
6657 return 0;
6659 return t1 == t2;
6662 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6663 given by TYPE. If the argument list accepts variable arguments,
6664 then this function counts only the ordinary arguments. */
6667 type_num_arguments (const_tree type)
6669 int i = 0;
6670 tree t;
6672 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6673 /* If the function does not take a variable number of arguments,
6674 the last element in the list will have type `void'. */
6675 if (VOID_TYPE_P (TREE_VALUE (t)))
6676 break;
6677 else
6678 ++i;
6680 return i;
6683 /* Nonzero if integer constants T1 and T2
6684 represent the same constant value. */
6687 tree_int_cst_equal (const_tree t1, const_tree t2)
6689 if (t1 == t2)
6690 return 1;
6692 if (t1 == 0 || t2 == 0)
6693 return 0;
6695 if (TREE_CODE (t1) == INTEGER_CST
6696 && TREE_CODE (t2) == INTEGER_CST
6697 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6698 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6699 return 1;
6701 return 0;
6704 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6705 The precise way of comparison depends on their data type. */
6708 tree_int_cst_lt (const_tree t1, const_tree t2)
6710 if (t1 == t2)
6711 return 0;
6713 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6715 int t1_sgn = tree_int_cst_sgn (t1);
6716 int t2_sgn = tree_int_cst_sgn (t2);
6718 if (t1_sgn < t2_sgn)
6719 return 1;
6720 else if (t1_sgn > t2_sgn)
6721 return 0;
6722 /* Otherwise, both are non-negative, so we compare them as
6723 unsigned just in case one of them would overflow a signed
6724 type. */
6726 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6727 return INT_CST_LT (t1, t2);
6729 return INT_CST_LT_UNSIGNED (t1, t2);
6732 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
6735 tree_int_cst_compare (const_tree t1, const_tree t2)
6737 if (tree_int_cst_lt (t1, t2))
6738 return -1;
6739 else if (tree_int_cst_lt (t2, t1))
6740 return 1;
6741 else
6742 return 0;
6745 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6746 the host. If POS is zero, the value can be represented in a single
6747 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
6748 be represented in a single unsigned HOST_WIDE_INT. */
6751 host_integerp (const_tree t, int pos)
6753 if (t == NULL_TREE)
6754 return 0;
6756 return (TREE_CODE (t) == INTEGER_CST
6757 && ((TREE_INT_CST_HIGH (t) == 0
6758 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6759 || (! pos && TREE_INT_CST_HIGH (t) == -1
6760 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6761 && !TYPE_UNSIGNED (TREE_TYPE (t)))
6762 || (pos && TREE_INT_CST_HIGH (t) == 0)));
6765 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6766 INTEGER_CST and there is no overflow. POS is nonzero if the result must
6767 be non-negative. We must be able to satisfy the above conditions. */
6769 HOST_WIDE_INT
6770 tree_low_cst (const_tree t, int pos)
6772 gcc_assert (host_integerp (t, pos));
6773 return TREE_INT_CST_LOW (t);
6776 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
6777 kind INTEGER_CST. This makes sure to properly sign-extend the
6778 constant. */
6780 HOST_WIDE_INT
6781 size_low_cst (const_tree t)
6783 double_int d = tree_to_double_int (t);
6784 return d.sext (TYPE_PRECISION (TREE_TYPE (t))).low;
6787 /* Return the most significant (sign) bit of T. */
6790 tree_int_cst_sign_bit (const_tree t)
6792 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
6793 unsigned HOST_WIDE_INT w;
6795 if (bitno < HOST_BITS_PER_WIDE_INT)
6796 w = TREE_INT_CST_LOW (t);
6797 else
6799 w = TREE_INT_CST_HIGH (t);
6800 bitno -= HOST_BITS_PER_WIDE_INT;
6803 return (w >> bitno) & 1;
6806 /* Return an indication of the sign of the integer constant T.
6807 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6808 Note that -1 will never be returned if T's type is unsigned. */
6811 tree_int_cst_sgn (const_tree t)
6813 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6814 return 0;
6815 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6816 return 1;
6817 else if (TREE_INT_CST_HIGH (t) < 0)
6818 return -1;
6819 else
6820 return 1;
6823 /* Return the minimum number of bits needed to represent VALUE in a
6824 signed or unsigned type, UNSIGNEDP says which. */
6826 unsigned int
6827 tree_int_cst_min_precision (tree value, bool unsignedp)
6829 /* If the value is negative, compute its negative minus 1. The latter
6830 adjustment is because the absolute value of the largest negative value
6831 is one larger than the largest positive value. This is equivalent to
6832 a bit-wise negation, so use that operation instead. */
6834 if (tree_int_cst_sgn (value) < 0)
6835 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6837 /* Return the number of bits needed, taking into account the fact
6838 that we need one more bit for a signed than unsigned type.
6839 If value is 0 or -1, the minimum precision is 1 no matter
6840 whether unsignedp is true or false. */
6842 if (integer_zerop (value))
6843 return 1;
6844 else
6845 return tree_floor_log2 (value) + 1 + !unsignedp;
6848 /* Compare two constructor-element-type constants. Return 1 if the lists
6849 are known to be equal; otherwise return 0. */
6852 simple_cst_list_equal (const_tree l1, const_tree l2)
6854 while (l1 != NULL_TREE && l2 != NULL_TREE)
6856 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6857 return 0;
6859 l1 = TREE_CHAIN (l1);
6860 l2 = TREE_CHAIN (l2);
6863 return l1 == l2;
6866 /* Return truthvalue of whether T1 is the same tree structure as T2.
6867 Return 1 if they are the same.
6868 Return 0 if they are understandably different.
6869 Return -1 if either contains tree structure not understood by
6870 this function. */
6873 simple_cst_equal (const_tree t1, const_tree t2)
6875 enum tree_code code1, code2;
6876 int cmp;
6877 int i;
6879 if (t1 == t2)
6880 return 1;
6881 if (t1 == 0 || t2 == 0)
6882 return 0;
6884 code1 = TREE_CODE (t1);
6885 code2 = TREE_CODE (t2);
6887 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6889 if (CONVERT_EXPR_CODE_P (code2)
6890 || code2 == NON_LVALUE_EXPR)
6891 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6892 else
6893 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6896 else if (CONVERT_EXPR_CODE_P (code2)
6897 || code2 == NON_LVALUE_EXPR)
6898 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6900 if (code1 != code2)
6901 return 0;
6903 switch (code1)
6905 case INTEGER_CST:
6906 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6907 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6909 case REAL_CST:
6910 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6912 case FIXED_CST:
6913 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6915 case STRING_CST:
6916 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6917 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6918 TREE_STRING_LENGTH (t1)));
6920 case CONSTRUCTOR:
6922 unsigned HOST_WIDE_INT idx;
6923 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
6924 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
6926 if (vec_safe_length (v1) != vec_safe_length (v2))
6927 return false;
6929 for (idx = 0; idx < vec_safe_length (v1); ++idx)
6930 /* ??? Should we handle also fields here? */
6931 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
6932 return false;
6933 return true;
6936 case SAVE_EXPR:
6937 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6939 case CALL_EXPR:
6940 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6941 if (cmp <= 0)
6942 return cmp;
6943 if (call_expr_nargs (t1) != call_expr_nargs (t2))
6944 return 0;
6946 const_tree arg1, arg2;
6947 const_call_expr_arg_iterator iter1, iter2;
6948 for (arg1 = first_const_call_expr_arg (t1, &iter1),
6949 arg2 = first_const_call_expr_arg (t2, &iter2);
6950 arg1 && arg2;
6951 arg1 = next_const_call_expr_arg (&iter1),
6952 arg2 = next_const_call_expr_arg (&iter2))
6954 cmp = simple_cst_equal (arg1, arg2);
6955 if (cmp <= 0)
6956 return cmp;
6958 return arg1 == arg2;
6961 case TARGET_EXPR:
6962 /* Special case: if either target is an unallocated VAR_DECL,
6963 it means that it's going to be unified with whatever the
6964 TARGET_EXPR is really supposed to initialize, so treat it
6965 as being equivalent to anything. */
6966 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6967 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6968 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6969 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6970 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6971 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6972 cmp = 1;
6973 else
6974 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6976 if (cmp <= 0)
6977 return cmp;
6979 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6981 case WITH_CLEANUP_EXPR:
6982 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6983 if (cmp <= 0)
6984 return cmp;
6986 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6988 case COMPONENT_REF:
6989 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6990 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6992 return 0;
6994 case VAR_DECL:
6995 case PARM_DECL:
6996 case CONST_DECL:
6997 case FUNCTION_DECL:
6998 return 0;
7000 default:
7001 break;
7004 /* This general rule works for most tree codes. All exceptions should be
7005 handled above. If this is a language-specific tree code, we can't
7006 trust what might be in the operand, so say we don't know
7007 the situation. */
7008 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7009 return -1;
7011 switch (TREE_CODE_CLASS (code1))
7013 case tcc_unary:
7014 case tcc_binary:
7015 case tcc_comparison:
7016 case tcc_expression:
7017 case tcc_reference:
7018 case tcc_statement:
7019 cmp = 1;
7020 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7022 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7023 if (cmp <= 0)
7024 return cmp;
7027 return cmp;
7029 default:
7030 return -1;
7034 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7035 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7036 than U, respectively. */
7039 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7041 if (tree_int_cst_sgn (t) < 0)
7042 return -1;
7043 else if (TREE_INT_CST_HIGH (t) != 0)
7044 return 1;
7045 else if (TREE_INT_CST_LOW (t) == u)
7046 return 0;
7047 else if (TREE_INT_CST_LOW (t) < u)
7048 return -1;
7049 else
7050 return 1;
7053 /* Return true if SIZE represents a constant size that is in bounds of
7054 what the middle-end and the backend accepts (covering not more than
7055 half of the address-space). */
7057 bool
7058 valid_constant_size_p (const_tree size)
7060 if (! host_integerp (size, 1)
7061 || TREE_OVERFLOW (size)
7062 || tree_int_cst_sign_bit (size) != 0)
7063 return false;
7064 return true;
7067 /* Return the precision of the type, or for a complex or vector type the
7068 precision of the type of its elements. */
7070 unsigned int
7071 element_precision (const_tree type)
7073 enum tree_code code = TREE_CODE (type);
7074 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7075 type = TREE_TYPE (type);
7077 return TYPE_PRECISION (type);
7080 /* Return true if CODE represents an associative tree code. Otherwise
7081 return false. */
7082 bool
7083 associative_tree_code (enum tree_code code)
7085 switch (code)
7087 case BIT_IOR_EXPR:
7088 case BIT_AND_EXPR:
7089 case BIT_XOR_EXPR:
7090 case PLUS_EXPR:
7091 case MULT_EXPR:
7092 case MIN_EXPR:
7093 case MAX_EXPR:
7094 return true;
7096 default:
7097 break;
7099 return false;
7102 /* Return true if CODE represents a commutative tree code. Otherwise
7103 return false. */
7104 bool
7105 commutative_tree_code (enum tree_code code)
7107 switch (code)
7109 case PLUS_EXPR:
7110 case MULT_EXPR:
7111 case MULT_HIGHPART_EXPR:
7112 case MIN_EXPR:
7113 case MAX_EXPR:
7114 case BIT_IOR_EXPR:
7115 case BIT_XOR_EXPR:
7116 case BIT_AND_EXPR:
7117 case NE_EXPR:
7118 case EQ_EXPR:
7119 case UNORDERED_EXPR:
7120 case ORDERED_EXPR:
7121 case UNEQ_EXPR:
7122 case LTGT_EXPR:
7123 case TRUTH_AND_EXPR:
7124 case TRUTH_XOR_EXPR:
7125 case TRUTH_OR_EXPR:
7126 case WIDEN_MULT_EXPR:
7127 case VEC_WIDEN_MULT_HI_EXPR:
7128 case VEC_WIDEN_MULT_LO_EXPR:
7129 case VEC_WIDEN_MULT_EVEN_EXPR:
7130 case VEC_WIDEN_MULT_ODD_EXPR:
7131 return true;
7133 default:
7134 break;
7136 return false;
7139 /* Return true if CODE represents a ternary tree code for which the
7140 first two operands are commutative. Otherwise return false. */
7141 bool
7142 commutative_ternary_tree_code (enum tree_code code)
7144 switch (code)
7146 case WIDEN_MULT_PLUS_EXPR:
7147 case WIDEN_MULT_MINUS_EXPR:
7148 return true;
7150 default:
7151 break;
7153 return false;
7156 /* Generate a hash value for an expression. This can be used iteratively
7157 by passing a previous result as the VAL argument.
7159 This function is intended to produce the same hash for expressions which
7160 would compare equal using operand_equal_p. */
7162 hashval_t
7163 iterative_hash_expr (const_tree t, hashval_t val)
7165 int i;
7166 enum tree_code code;
7167 char tclass;
7169 if (t == NULL_TREE)
7170 return iterative_hash_hashval_t (0, val);
7172 code = TREE_CODE (t);
7174 switch (code)
7176 /* Alas, constants aren't shared, so we can't rely on pointer
7177 identity. */
7178 case INTEGER_CST:
7179 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7180 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7181 case REAL_CST:
7183 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7185 return iterative_hash_hashval_t (val2, val);
7187 case FIXED_CST:
7189 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7191 return iterative_hash_hashval_t (val2, val);
7193 case STRING_CST:
7194 return iterative_hash (TREE_STRING_POINTER (t),
7195 TREE_STRING_LENGTH (t), val);
7196 case COMPLEX_CST:
7197 val = iterative_hash_expr (TREE_REALPART (t), val);
7198 return iterative_hash_expr (TREE_IMAGPART (t), val);
7199 case VECTOR_CST:
7201 unsigned i;
7202 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7203 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7204 return val;
7206 case SSA_NAME:
7207 /* We can just compare by pointer. */
7208 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7209 case PLACEHOLDER_EXPR:
7210 /* The node itself doesn't matter. */
7211 return val;
7212 case TREE_LIST:
7213 /* A list of expressions, for a CALL_EXPR or as the elements of a
7214 VECTOR_CST. */
7215 for (; t; t = TREE_CHAIN (t))
7216 val = iterative_hash_expr (TREE_VALUE (t), val);
7217 return val;
7218 case CONSTRUCTOR:
7220 unsigned HOST_WIDE_INT idx;
7221 tree field, value;
7222 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7224 val = iterative_hash_expr (field, val);
7225 val = iterative_hash_expr (value, val);
7227 return val;
7229 case MEM_REF:
7231 /* The type of the second operand is relevant, except for
7232 its top-level qualifiers. */
7233 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
7235 val = iterative_hash_object (TYPE_HASH (type), val);
7237 /* We could use the standard hash computation from this point
7238 on. */
7239 val = iterative_hash_object (code, val);
7240 val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
7241 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7242 return val;
7244 case FUNCTION_DECL:
7245 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7246 Otherwise nodes that compare equal according to operand_equal_p might
7247 get different hash codes. However, don't do this for machine specific
7248 or front end builtins, since the function code is overloaded in those
7249 cases. */
7250 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7251 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7253 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7254 code = TREE_CODE (t);
7256 /* FALL THROUGH */
7257 default:
7258 tclass = TREE_CODE_CLASS (code);
7260 if (tclass == tcc_declaration)
7262 /* DECL's have a unique ID */
7263 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7265 else
7267 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7269 val = iterative_hash_object (code, val);
7271 /* Don't hash the type, that can lead to having nodes which
7272 compare equal according to operand_equal_p, but which
7273 have different hash codes. */
7274 if (CONVERT_EXPR_CODE_P (code)
7275 || code == NON_LVALUE_EXPR)
7277 /* Make sure to include signness in the hash computation. */
7278 val += TYPE_UNSIGNED (TREE_TYPE (t));
7279 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7282 else if (commutative_tree_code (code))
7284 /* It's a commutative expression. We want to hash it the same
7285 however it appears. We do this by first hashing both operands
7286 and then rehashing based on the order of their independent
7287 hashes. */
7288 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7289 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7290 hashval_t t;
7292 if (one > two)
7293 t = one, one = two, two = t;
7295 val = iterative_hash_hashval_t (one, val);
7296 val = iterative_hash_hashval_t (two, val);
7298 else
7299 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7300 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7302 return val;
7306 /* Generate a hash value for a pair of expressions. This can be used
7307 iteratively by passing a previous result as the VAL argument.
7309 The same hash value is always returned for a given pair of expressions,
7310 regardless of the order in which they are presented. This is useful in
7311 hashing the operands of commutative functions. */
7313 hashval_t
7314 iterative_hash_exprs_commutative (const_tree t1,
7315 const_tree t2, hashval_t val)
7317 hashval_t one = iterative_hash_expr (t1, 0);
7318 hashval_t two = iterative_hash_expr (t2, 0);
7319 hashval_t t;
7321 if (one > two)
7322 t = one, one = two, two = t;
7323 val = iterative_hash_hashval_t (one, val);
7324 val = iterative_hash_hashval_t (two, val);
7326 return val;
7329 /* Constructors for pointer, array and function types.
7330 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7331 constructed by language-dependent code, not here.) */
7333 /* Construct, lay out and return the type of pointers to TO_TYPE with
7334 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7335 reference all of memory. If such a type has already been
7336 constructed, reuse it. */
7338 tree
7339 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7340 bool can_alias_all)
7342 tree t;
7344 if (to_type == error_mark_node)
7345 return error_mark_node;
7347 /* If the pointed-to type has the may_alias attribute set, force
7348 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7349 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7350 can_alias_all = true;
7352 /* In some cases, languages will have things that aren't a POINTER_TYPE
7353 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7354 In that case, return that type without regard to the rest of our
7355 operands.
7357 ??? This is a kludge, but consistent with the way this function has
7358 always operated and there doesn't seem to be a good way to avoid this
7359 at the moment. */
7360 if (TYPE_POINTER_TO (to_type) != 0
7361 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7362 return TYPE_POINTER_TO (to_type);
7364 /* First, if we already have a type for pointers to TO_TYPE and it's
7365 the proper mode, use it. */
7366 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7367 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7368 return t;
7370 t = make_node (POINTER_TYPE);
7372 TREE_TYPE (t) = to_type;
7373 SET_TYPE_MODE (t, mode);
7374 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7375 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7376 TYPE_POINTER_TO (to_type) = t;
7378 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7379 SET_TYPE_STRUCTURAL_EQUALITY (t);
7380 else if (TYPE_CANONICAL (to_type) != to_type)
7381 TYPE_CANONICAL (t)
7382 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7383 mode, can_alias_all);
7385 /* Lay out the type. This function has many callers that are concerned
7386 with expression-construction, and this simplifies them all. */
7387 layout_type (t);
7389 return t;
7392 /* By default build pointers in ptr_mode. */
7394 tree
7395 build_pointer_type (tree to_type)
7397 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7398 : TYPE_ADDR_SPACE (to_type);
7399 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7400 if (upc_shared_type_p (to_type))
7402 tree upc_pts_type;
7403 pointer_mode = TYPE_MODE (upc_pts_rep_type_node);
7404 upc_pts_type = build_pointer_type_for_mode (to_type, pointer_mode,
7405 false);
7406 TYPE_USER_ALIGN (upc_pts_type) = TYPE_USER_ALIGN (upc_pts_rep_type_node);
7407 TYPE_ALIGN (upc_pts_type) = TYPE_ALIGN (upc_pts_rep_type_node);
7408 return upc_pts_type;
7410 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7413 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7415 tree
7416 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7417 bool can_alias_all)
7419 tree t;
7421 if (to_type == error_mark_node)
7422 return error_mark_node;
7424 /* If the pointed-to type has the may_alias attribute set, force
7425 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7426 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7427 can_alias_all = true;
7429 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7430 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7431 In that case, return that type without regard to the rest of our
7432 operands.
7434 ??? This is a kludge, but consistent with the way this function has
7435 always operated and there doesn't seem to be a good way to avoid this
7436 at the moment. */
7437 if (TYPE_REFERENCE_TO (to_type) != 0
7438 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7439 return TYPE_REFERENCE_TO (to_type);
7441 /* First, if we already have a type for pointers to TO_TYPE and it's
7442 the proper mode, use it. */
7443 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7444 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7445 return t;
7447 t = make_node (REFERENCE_TYPE);
7449 TREE_TYPE (t) = to_type;
7450 SET_TYPE_MODE (t, mode);
7451 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7452 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7453 TYPE_REFERENCE_TO (to_type) = t;
7455 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7456 SET_TYPE_STRUCTURAL_EQUALITY (t);
7457 else if (TYPE_CANONICAL (to_type) != to_type)
7458 TYPE_CANONICAL (t)
7459 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7460 mode, can_alias_all);
7462 layout_type (t);
7464 return t;
7468 /* Build the node for the type of references-to-TO_TYPE by default
7469 in ptr_mode. */
7471 tree
7472 build_reference_type (tree to_type)
7474 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7475 : TYPE_ADDR_SPACE (to_type);
7476 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7477 return build_reference_type_for_mode (to_type, pointer_mode, false);
7480 /* Build a type that is compatible with t but has no cv quals anywhere
7481 in its type, thus
7483 const char *const *const * -> char ***. */
7485 tree
7486 build_type_no_quals (tree t)
7488 switch (TREE_CODE (t))
7490 case POINTER_TYPE:
7491 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7492 TYPE_MODE (t),
7493 TYPE_REF_CAN_ALIAS_ALL (t));
7494 case REFERENCE_TYPE:
7495 return
7496 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7497 TYPE_MODE (t),
7498 TYPE_REF_CAN_ALIAS_ALL (t));
7499 default:
7500 return TYPE_MAIN_VARIANT (t);
7504 #define MAX_INT_CACHED_PREC \
7505 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7506 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7508 /* Builds a signed or unsigned integer type of precision PRECISION.
7509 Used for C bitfields whose precision does not match that of
7510 built-in target types. */
7511 tree
7512 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7513 int unsignedp)
7515 tree itype, ret;
7517 if (unsignedp)
7518 unsignedp = MAX_INT_CACHED_PREC + 1;
7520 if (precision <= MAX_INT_CACHED_PREC)
7522 itype = nonstandard_integer_type_cache[precision + unsignedp];
7523 if (itype)
7524 return itype;
7527 itype = make_node (INTEGER_TYPE);
7528 TYPE_PRECISION (itype) = precision;
7530 if (unsignedp)
7531 fixup_unsigned_type (itype);
7532 else
7533 fixup_signed_type (itype);
7535 ret = itype;
7536 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7537 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7538 if (precision <= MAX_INT_CACHED_PREC)
7539 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7541 return ret;
7544 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7545 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7546 is true, reuse such a type that has already been constructed. */
7548 static tree
7549 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7551 tree itype = make_node (INTEGER_TYPE);
7552 hashval_t hashcode = 0;
7554 TREE_TYPE (itype) = type;
7556 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7557 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7559 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7560 SET_TYPE_MODE (itype, TYPE_MODE (type));
7561 TYPE_SIZE (itype) = TYPE_SIZE (type);
7562 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7563 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7564 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7566 if (!shared)
7567 return itype;
7569 if ((TYPE_MIN_VALUE (itype)
7570 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7571 || (TYPE_MAX_VALUE (itype)
7572 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7574 /* Since we cannot reliably merge this type, we need to compare it using
7575 structural equality checks. */
7576 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7577 return itype;
7580 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7581 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7582 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7583 itype = type_hash_canon (hashcode, itype);
7585 return itype;
7588 /* Wrapper around build_range_type_1 with SHARED set to true. */
7590 tree
7591 build_range_type (tree type, tree lowval, tree highval)
7593 return build_range_type_1 (type, lowval, highval, true);
7596 /* Wrapper around build_range_type_1 with SHARED set to false. */
7598 tree
7599 build_nonshared_range_type (tree type, tree lowval, tree highval)
7601 return build_range_type_1 (type, lowval, highval, false);
7604 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7605 MAXVAL should be the maximum value in the domain
7606 (one less than the length of the array).
7608 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7609 We don't enforce this limit, that is up to caller (e.g. language front end).
7610 The limit exists because the result is a signed type and we don't handle
7611 sizes that use more than one HOST_WIDE_INT. */
7613 tree
7614 build_index_type (tree maxval)
7616 return build_range_type (sizetype, size_zero_node, maxval);
7619 /* Return true if the debug information for TYPE, a subtype, should be emitted
7620 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7621 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7622 debug info and doesn't reflect the source code. */
7624 bool
7625 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7627 tree base_type = TREE_TYPE (type), low, high;
7629 /* Subrange types have a base type which is an integral type. */
7630 if (!INTEGRAL_TYPE_P (base_type))
7631 return false;
7633 /* Get the real bounds of the subtype. */
7634 if (lang_hooks.types.get_subrange_bounds)
7635 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7636 else
7638 low = TYPE_MIN_VALUE (type);
7639 high = TYPE_MAX_VALUE (type);
7642 /* If the type and its base type have the same representation and the same
7643 name, then the type is not a subrange but a copy of the base type. */
7644 if ((TREE_CODE (base_type) == INTEGER_TYPE
7645 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7646 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7647 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7648 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7650 tree type_name = TYPE_NAME (type);
7651 tree base_type_name = TYPE_NAME (base_type);
7653 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7654 type_name = DECL_NAME (type_name);
7656 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7657 base_type_name = DECL_NAME (base_type_name);
7659 if (type_name == base_type_name)
7660 return false;
7663 if (lowval)
7664 *lowval = low;
7665 if (highval)
7666 *highval = high;
7667 return true;
7670 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7671 and number of elements specified by the range of values of INDEX_TYPE.
7672 If SHARED is true, reuse such a type that has already been constructed. */
7674 static tree
7675 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7677 tree t;
7679 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7681 error ("arrays of functions are not meaningful");
7682 elt_type = integer_type_node;
7685 t = make_node (ARRAY_TYPE);
7686 TREE_TYPE (t) = elt_type;
7687 TYPE_DOMAIN (t) = index_type;
7688 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7689 layout_type (t);
7691 /* If the element type is incomplete at this point we get marked for
7692 structural equality. Do not record these types in the canonical
7693 type hashtable. */
7694 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7695 return t;
7697 if (shared)
7699 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7700 if (index_type)
7701 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7702 t = type_hash_canon (hashcode, t);
7705 if (TYPE_CANONICAL (t) == t)
7707 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7708 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7709 SET_TYPE_STRUCTURAL_EQUALITY (t);
7710 else if (TYPE_CANONICAL (elt_type) != elt_type
7711 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7712 TYPE_CANONICAL (t)
7713 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7714 index_type
7715 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7716 shared);
7719 return t;
7722 /* Wrapper around build_array_type_1 with SHARED set to true. */
7724 tree
7725 build_array_type (tree elt_type, tree index_type)
7727 return build_array_type_1 (elt_type, index_type, true);
7730 /* Wrapper around build_array_type_1 with SHARED set to false. */
7732 tree
7733 build_nonshared_array_type (tree elt_type, tree index_type)
7735 return build_array_type_1 (elt_type, index_type, false);
7738 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7739 sizetype. */
7741 tree
7742 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7744 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7747 /* Recursively examines the array elements of TYPE, until a non-array
7748 element type is found. */
7750 tree
7751 strip_array_types (tree type)
7753 while (TREE_CODE (type) == ARRAY_TYPE)
7754 type = TREE_TYPE (type);
7756 return type;
7759 /* Computes the canonical argument types from the argument type list
7760 ARGTYPES.
7762 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7763 on entry to this function, or if any of the ARGTYPES are
7764 structural.
7766 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7767 true on entry to this function, or if any of the ARGTYPES are
7768 non-canonical.
7770 Returns a canonical argument list, which may be ARGTYPES when the
7771 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7772 true) or would not differ from ARGTYPES. */
7774 static tree
7775 maybe_canonicalize_argtypes(tree argtypes,
7776 bool *any_structural_p,
7777 bool *any_noncanonical_p)
7779 tree arg;
7780 bool any_noncanonical_argtypes_p = false;
7782 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7784 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7785 /* Fail gracefully by stating that the type is structural. */
7786 *any_structural_p = true;
7787 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7788 *any_structural_p = true;
7789 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7790 || TREE_PURPOSE (arg))
7791 /* If the argument has a default argument, we consider it
7792 non-canonical even though the type itself is canonical.
7793 That way, different variants of function and method types
7794 with default arguments will all point to the variant with
7795 no defaults as their canonical type. */
7796 any_noncanonical_argtypes_p = true;
7799 if (*any_structural_p)
7800 return argtypes;
7802 if (any_noncanonical_argtypes_p)
7804 /* Build the canonical list of argument types. */
7805 tree canon_argtypes = NULL_TREE;
7806 bool is_void = false;
7808 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7810 if (arg == void_list_node)
7811 is_void = true;
7812 else
7813 canon_argtypes = tree_cons (NULL_TREE,
7814 TYPE_CANONICAL (TREE_VALUE (arg)),
7815 canon_argtypes);
7818 canon_argtypes = nreverse (canon_argtypes);
7819 if (is_void)
7820 canon_argtypes = chainon (canon_argtypes, void_list_node);
7822 /* There is a non-canonical type. */
7823 *any_noncanonical_p = true;
7824 return canon_argtypes;
7827 /* The canonical argument types are the same as ARGTYPES. */
7828 return argtypes;
7831 /* Construct, lay out and return
7832 the type of functions returning type VALUE_TYPE
7833 given arguments of types ARG_TYPES.
7834 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7835 are data type nodes for the arguments of the function.
7836 If such a type has already been constructed, reuse it. */
7838 tree
7839 build_function_type (tree value_type, tree arg_types)
7841 tree t;
7842 hashval_t hashcode = 0;
7843 bool any_structural_p, any_noncanonical_p;
7844 tree canon_argtypes;
7846 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7848 error ("function return type cannot be function");
7849 value_type = integer_type_node;
7852 /* Make a node of the sort we want. */
7853 t = make_node (FUNCTION_TYPE);
7854 TREE_TYPE (t) = value_type;
7855 TYPE_ARG_TYPES (t) = arg_types;
7857 /* If we already have such a type, use the old one. */
7858 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7859 hashcode = type_hash_list (arg_types, hashcode);
7860 t = type_hash_canon (hashcode, t);
7862 /* Set up the canonical type. */
7863 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7864 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7865 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7866 &any_structural_p,
7867 &any_noncanonical_p);
7868 if (any_structural_p)
7869 SET_TYPE_STRUCTURAL_EQUALITY (t);
7870 else if (any_noncanonical_p)
7871 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7872 canon_argtypes);
7874 if (!COMPLETE_TYPE_P (t))
7875 layout_type (t);
7876 return t;
7879 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7880 return value if SKIP_RETURN is true. */
7882 static tree
7883 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7884 bool skip_return)
7886 tree new_type = NULL;
7887 tree args, new_args = NULL, t;
7888 tree new_reversed;
7889 int i = 0;
7891 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7892 args = TREE_CHAIN (args), i++)
7893 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7894 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7896 new_reversed = nreverse (new_args);
7897 if (args)
7899 if (new_reversed)
7900 TREE_CHAIN (new_args) = void_list_node;
7901 else
7902 new_reversed = void_list_node;
7905 /* Use copy_node to preserve as much as possible from original type
7906 (debug info, attribute lists etc.)
7907 Exception is METHOD_TYPEs must have THIS argument.
7908 When we are asked to remove it, we need to build new FUNCTION_TYPE
7909 instead. */
7910 if (TREE_CODE (orig_type) != METHOD_TYPE
7911 || !args_to_skip
7912 || !bitmap_bit_p (args_to_skip, 0))
7914 new_type = build_distinct_type_copy (orig_type);
7915 TYPE_ARG_TYPES (new_type) = new_reversed;
7917 else
7919 new_type
7920 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7921 new_reversed));
7922 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7925 if (skip_return)
7926 TREE_TYPE (new_type) = void_type_node;
7928 /* This is a new type, not a copy of an old type. Need to reassociate
7929 variants. We can handle everything except the main variant lazily. */
7930 t = TYPE_MAIN_VARIANT (orig_type);
7931 if (t != orig_type)
7933 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7934 TYPE_MAIN_VARIANT (new_type) = t;
7935 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7936 TYPE_NEXT_VARIANT (t) = new_type;
7938 else
7940 TYPE_MAIN_VARIANT (new_type) = new_type;
7941 TYPE_NEXT_VARIANT (new_type) = NULL;
7944 return new_type;
7947 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7948 return value if SKIP_RETURN is true.
7950 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7951 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7952 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7954 tree
7955 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7956 bool skip_return)
7958 tree new_decl = copy_node (orig_decl);
7959 tree new_type;
7961 new_type = TREE_TYPE (orig_decl);
7962 if (prototype_p (new_type)
7963 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7964 new_type
7965 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7966 TREE_TYPE (new_decl) = new_type;
7968 /* For declarations setting DECL_VINDEX (i.e. methods)
7969 we expect first argument to be THIS pointer. */
7970 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7971 DECL_VINDEX (new_decl) = NULL_TREE;
7973 /* When signature changes, we need to clear builtin info. */
7974 if (DECL_BUILT_IN (new_decl)
7975 && args_to_skip
7976 && !bitmap_empty_p (args_to_skip))
7978 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7979 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7981 return new_decl;
7984 /* Build a function type. The RETURN_TYPE is the type returned by the
7985 function. If VAARGS is set, no void_type_node is appended to the
7986 the list. ARGP must be always be terminated be a NULL_TREE. */
7988 static tree
7989 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7991 tree t, args, last;
7993 t = va_arg (argp, tree);
7994 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7995 args = tree_cons (NULL_TREE, t, args);
7997 if (vaargs)
7999 last = args;
8000 if (args != NULL_TREE)
8001 args = nreverse (args);
8002 gcc_assert (last != void_list_node);
8004 else if (args == NULL_TREE)
8005 args = void_list_node;
8006 else
8008 last = args;
8009 args = nreverse (args);
8010 TREE_CHAIN (last) = void_list_node;
8012 args = build_function_type (return_type, args);
8014 return args;
8017 /* Build a function type. The RETURN_TYPE is the type returned by the
8018 function. If additional arguments are provided, they are
8019 additional argument types. The list of argument types must always
8020 be terminated by NULL_TREE. */
8022 tree
8023 build_function_type_list (tree return_type, ...)
8025 tree args;
8026 va_list p;
8028 va_start (p, return_type);
8029 args = build_function_type_list_1 (false, return_type, p);
8030 va_end (p);
8031 return args;
8034 /* Build a variable argument function type. The RETURN_TYPE is the
8035 type returned by the function. If additional arguments are provided,
8036 they are additional argument types. The list of argument types must
8037 always be terminated by NULL_TREE. */
8039 tree
8040 build_varargs_function_type_list (tree return_type, ...)
8042 tree args;
8043 va_list p;
8045 va_start (p, return_type);
8046 args = build_function_type_list_1 (true, return_type, p);
8047 va_end (p);
8049 return args;
8052 /* Build a function type. RETURN_TYPE is the type returned by the
8053 function; VAARGS indicates whether the function takes varargs. The
8054 function takes N named arguments, the types of which are provided in
8055 ARG_TYPES. */
8057 static tree
8058 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8059 tree *arg_types)
8061 int i;
8062 tree t = vaargs ? NULL_TREE : void_list_node;
8064 for (i = n - 1; i >= 0; i--)
8065 t = tree_cons (NULL_TREE, arg_types[i], t);
8067 return build_function_type (return_type, t);
8070 /* Build a function type. RETURN_TYPE is the type returned by the
8071 function. The function takes N named arguments, the types of which
8072 are provided in ARG_TYPES. */
8074 tree
8075 build_function_type_array (tree return_type, int n, tree *arg_types)
8077 return build_function_type_array_1 (false, return_type, n, arg_types);
8080 /* Build a variable argument function type. RETURN_TYPE is the type
8081 returned by the function. The function takes N named arguments, the
8082 types of which are provided in ARG_TYPES. */
8084 tree
8085 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8087 return build_function_type_array_1 (true, return_type, n, arg_types);
8090 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8091 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8092 for the method. An implicit additional parameter (of type
8093 pointer-to-BASETYPE) is added to the ARGTYPES. */
8095 tree
8096 build_method_type_directly (tree basetype,
8097 tree rettype,
8098 tree argtypes)
8100 tree t;
8101 tree ptype;
8102 int hashcode = 0;
8103 bool any_structural_p, any_noncanonical_p;
8104 tree canon_argtypes;
8106 /* Make a node of the sort we want. */
8107 t = make_node (METHOD_TYPE);
8109 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8110 TREE_TYPE (t) = rettype;
8111 ptype = build_pointer_type (basetype);
8113 /* The actual arglist for this function includes a "hidden" argument
8114 which is "this". Put it into the list of argument types. */
8115 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8116 TYPE_ARG_TYPES (t) = argtypes;
8118 /* If we already have such a type, use the old one. */
8119 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8120 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8121 hashcode = type_hash_list (argtypes, hashcode);
8122 t = type_hash_canon (hashcode, t);
8124 /* Set up the canonical type. */
8125 any_structural_p
8126 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8127 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8128 any_noncanonical_p
8129 = (TYPE_CANONICAL (basetype) != basetype
8130 || TYPE_CANONICAL (rettype) != rettype);
8131 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8132 &any_structural_p,
8133 &any_noncanonical_p);
8134 if (any_structural_p)
8135 SET_TYPE_STRUCTURAL_EQUALITY (t);
8136 else if (any_noncanonical_p)
8137 TYPE_CANONICAL (t)
8138 = build_method_type_directly (TYPE_CANONICAL (basetype),
8139 TYPE_CANONICAL (rettype),
8140 canon_argtypes);
8141 if (!COMPLETE_TYPE_P (t))
8142 layout_type (t);
8144 return t;
8147 /* Construct, lay out and return the type of methods belonging to class
8148 BASETYPE and whose arguments and values are described by TYPE.
8149 If that type exists already, reuse it.
8150 TYPE must be a FUNCTION_TYPE node. */
8152 tree
8153 build_method_type (tree basetype, tree type)
8155 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8157 return build_method_type_directly (basetype,
8158 TREE_TYPE (type),
8159 TYPE_ARG_TYPES (type));
8162 /* Construct, lay out and return the type of offsets to a value
8163 of type TYPE, within an object of type BASETYPE.
8164 If a suitable offset type exists already, reuse it. */
8166 tree
8167 build_offset_type (tree basetype, tree type)
8169 tree t;
8170 hashval_t hashcode = 0;
8172 /* Make a node of the sort we want. */
8173 t = make_node (OFFSET_TYPE);
8175 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8176 TREE_TYPE (t) = type;
8178 /* If we already have such a type, use the old one. */
8179 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8180 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8181 t = type_hash_canon (hashcode, t);
8183 if (!COMPLETE_TYPE_P (t))
8184 layout_type (t);
8186 if (TYPE_CANONICAL (t) == t)
8188 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8189 || TYPE_STRUCTURAL_EQUALITY_P (type))
8190 SET_TYPE_STRUCTURAL_EQUALITY (t);
8191 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8192 || TYPE_CANONICAL (type) != type)
8193 TYPE_CANONICAL (t)
8194 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8195 TYPE_CANONICAL (type));
8198 return t;
8201 /* Create a complex type whose components are COMPONENT_TYPE. */
8203 tree
8204 build_complex_type (tree component_type)
8206 tree t;
8207 hashval_t hashcode;
8209 gcc_assert (INTEGRAL_TYPE_P (component_type)
8210 || SCALAR_FLOAT_TYPE_P (component_type)
8211 || FIXED_POINT_TYPE_P (component_type));
8213 /* Make a node of the sort we want. */
8214 t = make_node (COMPLEX_TYPE);
8216 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8218 /* If we already have such a type, use the old one. */
8219 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8220 t = type_hash_canon (hashcode, t);
8222 if (!COMPLETE_TYPE_P (t))
8223 layout_type (t);
8225 if (TYPE_CANONICAL (t) == t)
8227 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8228 SET_TYPE_STRUCTURAL_EQUALITY (t);
8229 else if (TYPE_CANONICAL (component_type) != component_type)
8230 TYPE_CANONICAL (t)
8231 = build_complex_type (TYPE_CANONICAL (component_type));
8234 /* We need to create a name, since complex is a fundamental type. */
8235 if (! TYPE_NAME (t))
8237 const char *name;
8238 if (component_type == char_type_node)
8239 name = "complex char";
8240 else if (component_type == signed_char_type_node)
8241 name = "complex signed char";
8242 else if (component_type == unsigned_char_type_node)
8243 name = "complex unsigned char";
8244 else if (component_type == short_integer_type_node)
8245 name = "complex short int";
8246 else if (component_type == short_unsigned_type_node)
8247 name = "complex short unsigned int";
8248 else if (component_type == integer_type_node)
8249 name = "complex int";
8250 else if (component_type == unsigned_type_node)
8251 name = "complex unsigned int";
8252 else if (component_type == long_integer_type_node)
8253 name = "complex long int";
8254 else if (component_type == long_unsigned_type_node)
8255 name = "complex long unsigned int";
8256 else if (component_type == long_long_integer_type_node)
8257 name = "complex long long int";
8258 else if (component_type == long_long_unsigned_type_node)
8259 name = "complex long long unsigned int";
8260 else
8261 name = 0;
8263 if (name != 0)
8264 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8265 get_identifier (name), t);
8268 return build_qualified_type (t, TYPE_QUALS (component_type));
8271 /* If TYPE is a real or complex floating-point type and the target
8272 does not directly support arithmetic on TYPE then return the wider
8273 type to be used for arithmetic on TYPE. Otherwise, return
8274 NULL_TREE. */
8276 tree
8277 excess_precision_type (tree type)
8279 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8281 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8282 switch (TREE_CODE (type))
8284 case REAL_TYPE:
8285 switch (flt_eval_method)
8287 case 1:
8288 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8289 return double_type_node;
8290 break;
8291 case 2:
8292 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8293 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8294 return long_double_type_node;
8295 break;
8296 default:
8297 gcc_unreachable ();
8299 break;
8300 case COMPLEX_TYPE:
8301 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8302 return NULL_TREE;
8303 switch (flt_eval_method)
8305 case 1:
8306 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8307 return complex_double_type_node;
8308 break;
8309 case 2:
8310 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8311 || (TYPE_MODE (TREE_TYPE (type))
8312 == TYPE_MODE (double_type_node)))
8313 return complex_long_double_type_node;
8314 break;
8315 default:
8316 gcc_unreachable ();
8318 break;
8319 default:
8320 break;
8323 return NULL_TREE;
8326 /* Return OP, stripped of any conversions to wider types as much as is safe.
8327 Converting the value back to OP's type makes a value equivalent to OP.
8329 If FOR_TYPE is nonzero, we return a value which, if converted to
8330 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8332 OP must have integer, real or enumeral type. Pointers are not allowed!
8334 There are some cases where the obvious value we could return
8335 would regenerate to OP if converted to OP's type,
8336 but would not extend like OP to wider types.
8337 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8338 For example, if OP is (unsigned short)(signed char)-1,
8339 we avoid returning (signed char)-1 if FOR_TYPE is int,
8340 even though extending that to an unsigned short would regenerate OP,
8341 since the result of extending (signed char)-1 to (int)
8342 is different from (int) OP. */
8344 tree
8345 get_unwidened (tree op, tree for_type)
8347 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8348 tree type = TREE_TYPE (op);
8349 unsigned final_prec
8350 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8351 int uns
8352 = (for_type != 0 && for_type != type
8353 && final_prec > TYPE_PRECISION (type)
8354 && TYPE_UNSIGNED (type));
8355 tree win = op;
8357 while (CONVERT_EXPR_P (op))
8359 int bitschange;
8361 /* TYPE_PRECISION on vector types has different meaning
8362 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8363 so avoid them here. */
8364 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8365 break;
8367 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8368 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8370 /* Truncations are many-one so cannot be removed.
8371 Unless we are later going to truncate down even farther. */
8372 if (bitschange < 0
8373 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8374 break;
8376 /* See what's inside this conversion. If we decide to strip it,
8377 we will set WIN. */
8378 op = TREE_OPERAND (op, 0);
8380 /* If we have not stripped any zero-extensions (uns is 0),
8381 we can strip any kind of extension.
8382 If we have previously stripped a zero-extension,
8383 only zero-extensions can safely be stripped.
8384 Any extension can be stripped if the bits it would produce
8385 are all going to be discarded later by truncating to FOR_TYPE. */
8387 if (bitschange > 0)
8389 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8390 win = op;
8391 /* TYPE_UNSIGNED says whether this is a zero-extension.
8392 Let's avoid computing it if it does not affect WIN
8393 and if UNS will not be needed again. */
8394 if ((uns
8395 || CONVERT_EXPR_P (op))
8396 && TYPE_UNSIGNED (TREE_TYPE (op)))
8398 uns = 1;
8399 win = op;
8404 /* If we finally reach a constant see if it fits in for_type and
8405 in that case convert it. */
8406 if (for_type
8407 && TREE_CODE (win) == INTEGER_CST
8408 && TREE_TYPE (win) != for_type
8409 && int_fits_type_p (win, for_type))
8410 win = fold_convert (for_type, win);
8412 return win;
8415 /* Return OP or a simpler expression for a narrower value
8416 which can be sign-extended or zero-extended to give back OP.
8417 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8418 or 0 if the value should be sign-extended. */
8420 tree
8421 get_narrower (tree op, int *unsignedp_ptr)
8423 int uns = 0;
8424 int first = 1;
8425 tree win = op;
8426 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8428 while (TREE_CODE (op) == NOP_EXPR)
8430 int bitschange
8431 = (TYPE_PRECISION (TREE_TYPE (op))
8432 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8434 /* Truncations are many-one so cannot be removed. */
8435 if (bitschange < 0)
8436 break;
8438 /* See what's inside this conversion. If we decide to strip it,
8439 we will set WIN. */
8441 if (bitschange > 0)
8443 op = TREE_OPERAND (op, 0);
8444 /* An extension: the outermost one can be stripped,
8445 but remember whether it is zero or sign extension. */
8446 if (first)
8447 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8448 /* Otherwise, if a sign extension has been stripped,
8449 only sign extensions can now be stripped;
8450 if a zero extension has been stripped, only zero-extensions. */
8451 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8452 break;
8453 first = 0;
8455 else /* bitschange == 0 */
8457 /* A change in nominal type can always be stripped, but we must
8458 preserve the unsignedness. */
8459 if (first)
8460 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8461 first = 0;
8462 op = TREE_OPERAND (op, 0);
8463 /* Keep trying to narrow, but don't assign op to win if it
8464 would turn an integral type into something else. */
8465 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8466 continue;
8469 win = op;
8472 if (TREE_CODE (op) == COMPONENT_REF
8473 /* Since type_for_size always gives an integer type. */
8474 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8475 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8476 /* Ensure field is laid out already. */
8477 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8478 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8480 unsigned HOST_WIDE_INT innerprec
8481 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8482 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8483 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8484 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8486 /* We can get this structure field in a narrower type that fits it,
8487 but the resulting extension to its nominal type (a fullword type)
8488 must satisfy the same conditions as for other extensions.
8490 Do this only for fields that are aligned (not bit-fields),
8491 because when bit-field insns will be used there is no
8492 advantage in doing this. */
8494 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8495 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8496 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8497 && type != 0)
8499 if (first)
8500 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8501 win = fold_convert (type, op);
8505 *unsignedp_ptr = uns;
8506 return win;
8509 /* Returns true if integer constant C has a value that is permissible
8510 for type TYPE (an INTEGER_TYPE). */
8512 bool
8513 int_fits_type_p (const_tree c, const_tree type)
8515 tree type_low_bound, type_high_bound;
8516 bool ok_for_low_bound, ok_for_high_bound, unsc;
8517 double_int dc, dd;
8519 dc = tree_to_double_int (c);
8520 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8522 retry:
8523 type_low_bound = TYPE_MIN_VALUE (type);
8524 type_high_bound = TYPE_MAX_VALUE (type);
8526 /* If at least one bound of the type is a constant integer, we can check
8527 ourselves and maybe make a decision. If no such decision is possible, but
8528 this type is a subtype, try checking against that. Otherwise, use
8529 double_int_fits_to_tree_p, which checks against the precision.
8531 Compute the status for each possibly constant bound, and return if we see
8532 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8533 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8534 for "constant known to fit". */
8536 /* Check if c >= type_low_bound. */
8537 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8539 dd = tree_to_double_int (type_low_bound);
8540 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8542 int c_neg = (!unsc && dc.is_negative ());
8543 int t_neg = (unsc && dd.is_negative ());
8545 if (c_neg && !t_neg)
8546 return false;
8547 if ((c_neg || !t_neg) && dc.ult (dd))
8548 return false;
8550 else if (dc.cmp (dd, unsc) < 0)
8551 return false;
8552 ok_for_low_bound = true;
8554 else
8555 ok_for_low_bound = false;
8557 /* Check if c <= type_high_bound. */
8558 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8560 dd = tree_to_double_int (type_high_bound);
8561 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8563 int c_neg = (!unsc && dc.is_negative ());
8564 int t_neg = (unsc && dd.is_negative ());
8566 if (t_neg && !c_neg)
8567 return false;
8568 if ((t_neg || !c_neg) && dc.ugt (dd))
8569 return false;
8571 else if (dc.cmp (dd, unsc) > 0)
8572 return false;
8573 ok_for_high_bound = true;
8575 else
8576 ok_for_high_bound = false;
8578 /* If the constant fits both bounds, the result is known. */
8579 if (ok_for_low_bound && ok_for_high_bound)
8580 return true;
8582 /* Perform some generic filtering which may allow making a decision
8583 even if the bounds are not constant. First, negative integers
8584 never fit in unsigned types, */
8585 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8586 return false;
8588 /* Second, narrower types always fit in wider ones. */
8589 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8590 return true;
8592 /* Third, unsigned integers with top bit set never fit signed types. */
8593 if (! TYPE_UNSIGNED (type) && unsc)
8595 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8596 if (prec < HOST_BITS_PER_WIDE_INT)
8598 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8599 return false;
8601 else if (((((unsigned HOST_WIDE_INT) 1)
8602 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8603 return false;
8606 /* If we haven't been able to decide at this point, there nothing more we
8607 can check ourselves here. Look at the base type if we have one and it
8608 has the same precision. */
8609 if (TREE_CODE (type) == INTEGER_TYPE
8610 && TREE_TYPE (type) != 0
8611 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8613 type = TREE_TYPE (type);
8614 goto retry;
8617 /* Or to double_int_fits_to_tree_p, if nothing else. */
8618 return double_int_fits_to_tree_p (type, dc);
8621 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8622 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8623 represented (assuming two's-complement arithmetic) within the bit
8624 precision of the type are returned instead. */
8626 void
8627 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8629 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8630 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8631 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8632 TYPE_UNSIGNED (type));
8633 else
8635 if (TYPE_UNSIGNED (type))
8636 mpz_set_ui (min, 0);
8637 else
8639 double_int mn;
8640 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8641 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8642 mpz_set_double_int (min, mn, false);
8646 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8647 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8648 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8649 TYPE_UNSIGNED (type));
8650 else
8652 if (TYPE_UNSIGNED (type))
8653 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8654 true);
8655 else
8656 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8657 true);
8661 /* Return true if VAR is an automatic variable defined in function FN. */
8663 bool
8664 auto_var_in_fn_p (const_tree var, const_tree fn)
8666 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8667 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8668 || TREE_CODE (var) == PARM_DECL)
8669 && ! TREE_STATIC (var))
8670 || TREE_CODE (var) == LABEL_DECL
8671 || TREE_CODE (var) == RESULT_DECL));
8674 /* Subprogram of following function. Called by walk_tree.
8676 Return *TP if it is an automatic variable or parameter of the
8677 function passed in as DATA. */
8679 static tree
8680 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8682 tree fn = (tree) data;
8684 if (TYPE_P (*tp))
8685 *walk_subtrees = 0;
8687 else if (DECL_P (*tp)
8688 && auto_var_in_fn_p (*tp, fn))
8689 return *tp;
8691 return NULL_TREE;
8694 /* Returns true if T is, contains, or refers to a type with variable
8695 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8696 arguments, but not the return type. If FN is nonzero, only return
8697 true if a modifier of the type or position of FN is a variable or
8698 parameter inside FN.
8700 This concept is more general than that of C99 'variably modified types':
8701 in C99, a struct type is never variably modified because a VLA may not
8702 appear as a structure member. However, in GNU C code like:
8704 struct S { int i[f()]; };
8706 is valid, and other languages may define similar constructs. */
8708 bool
8709 variably_modified_type_p (tree type, tree fn)
8711 tree t;
8713 /* Test if T is either variable (if FN is zero) or an expression containing
8714 a variable in FN. If TYPE isn't gimplified, return true also if
8715 gimplify_one_sizepos would gimplify the expression into a local
8716 variable. */
8717 #define RETURN_TRUE_IF_VAR(T) \
8718 do { tree _t = (T); \
8719 if (_t != NULL_TREE \
8720 && _t != error_mark_node \
8721 && TREE_CODE (_t) != INTEGER_CST \
8722 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8723 && (!fn \
8724 || (!TYPE_SIZES_GIMPLIFIED (type) \
8725 && !is_gimple_sizepos (_t)) \
8726 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8727 return true; } while (0)
8729 if (type == error_mark_node)
8730 return false;
8732 /* If TYPE itself has variable size, it is variably modified. */
8733 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8734 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8736 switch (TREE_CODE (type))
8738 case POINTER_TYPE:
8739 case REFERENCE_TYPE:
8740 case VECTOR_TYPE:
8741 if (variably_modified_type_p (TREE_TYPE (type), fn))
8742 return true;
8743 break;
8745 case FUNCTION_TYPE:
8746 case METHOD_TYPE:
8747 /* If TYPE is a function type, it is variably modified if the
8748 return type is variably modified. */
8749 if (variably_modified_type_p (TREE_TYPE (type), fn))
8750 return true;
8751 break;
8753 case INTEGER_TYPE:
8754 case REAL_TYPE:
8755 case FIXED_POINT_TYPE:
8756 case ENUMERAL_TYPE:
8757 case BOOLEAN_TYPE:
8758 /* Scalar types are variably modified if their end points
8759 aren't constant. */
8760 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8761 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8762 break;
8764 case RECORD_TYPE:
8765 case UNION_TYPE:
8766 case QUAL_UNION_TYPE:
8767 /* We can't see if any of the fields are variably-modified by the
8768 definition we normally use, since that would produce infinite
8769 recursion via pointers. */
8770 /* This is variably modified if some field's type is. */
8771 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8772 if (TREE_CODE (t) == FIELD_DECL)
8774 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8775 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8776 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8778 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8779 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8781 break;
8783 case ARRAY_TYPE:
8784 /* Do not call ourselves to avoid infinite recursion. This is
8785 variably modified if the element type is. */
8786 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8787 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8788 break;
8790 default:
8791 break;
8794 /* The current language may have other cases to check, but in general,
8795 all other types are not variably modified. */
8796 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8798 #undef RETURN_TRUE_IF_VAR
8801 /* Given a DECL or TYPE, return the scope in which it was declared, or
8802 NULL_TREE if there is no containing scope. */
8804 tree
8805 get_containing_scope (const_tree t)
8807 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8810 /* Return the innermost context enclosing DECL that is
8811 a FUNCTION_DECL, or zero if none. */
8813 tree
8814 decl_function_context (const_tree decl)
8816 tree context;
8818 if (TREE_CODE (decl) == ERROR_MARK)
8819 return 0;
8821 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8822 where we look up the function at runtime. Such functions always take
8823 a first argument of type 'pointer to real context'.
8825 C++ should really be fixed to use DECL_CONTEXT for the real context,
8826 and use something else for the "virtual context". */
8827 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8828 context
8829 = TYPE_MAIN_VARIANT
8830 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8831 else
8832 context = DECL_CONTEXT (decl);
8834 while (context && TREE_CODE (context) != FUNCTION_DECL)
8836 if (TREE_CODE (context) == BLOCK)
8837 context = BLOCK_SUPERCONTEXT (context);
8838 else
8839 context = get_containing_scope (context);
8842 return context;
8845 /* Return the innermost context enclosing DECL that is
8846 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8847 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8849 tree
8850 decl_type_context (const_tree decl)
8852 tree context = DECL_CONTEXT (decl);
8854 while (context)
8855 switch (TREE_CODE (context))
8857 case NAMESPACE_DECL:
8858 case TRANSLATION_UNIT_DECL:
8859 return NULL_TREE;
8861 case RECORD_TYPE:
8862 case UNION_TYPE:
8863 case QUAL_UNION_TYPE:
8864 return context;
8866 case TYPE_DECL:
8867 case FUNCTION_DECL:
8868 context = DECL_CONTEXT (context);
8869 break;
8871 case BLOCK:
8872 context = BLOCK_SUPERCONTEXT (context);
8873 break;
8875 default:
8876 gcc_unreachable ();
8879 return NULL_TREE;
8882 /* CALL is a CALL_EXPR. Return the declaration for the function
8883 called, or NULL_TREE if the called function cannot be
8884 determined. */
8886 tree
8887 get_callee_fndecl (const_tree call)
8889 tree addr;
8891 if (call == error_mark_node)
8892 return error_mark_node;
8894 /* It's invalid to call this function with anything but a
8895 CALL_EXPR. */
8896 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8898 /* The first operand to the CALL is the address of the function
8899 called. */
8900 addr = CALL_EXPR_FN (call);
8902 STRIP_NOPS (addr);
8904 /* If this is a readonly function pointer, extract its initial value. */
8905 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8906 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8907 && DECL_INITIAL (addr))
8908 addr = DECL_INITIAL (addr);
8910 /* If the address is just `&f' for some function `f', then we know
8911 that `f' is being called. */
8912 if (TREE_CODE (addr) == ADDR_EXPR
8913 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8914 return TREE_OPERAND (addr, 0);
8916 /* We couldn't figure out what was being called. */
8917 return NULL_TREE;
8920 /* Print debugging information about tree nodes generated during the compile,
8921 and any language-specific information. */
8923 void
8924 dump_tree_statistics (void)
8926 if (GATHER_STATISTICS)
8928 int i;
8929 int total_nodes, total_bytes;
8930 fprintf (stderr, "Kind Nodes Bytes\n");
8931 fprintf (stderr, "---------------------------------------\n");
8932 total_nodes = total_bytes = 0;
8933 for (i = 0; i < (int) all_kinds; i++)
8935 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8936 tree_node_counts[i], tree_node_sizes[i]);
8937 total_nodes += tree_node_counts[i];
8938 total_bytes += tree_node_sizes[i];
8940 fprintf (stderr, "---------------------------------------\n");
8941 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8942 fprintf (stderr, "---------------------------------------\n");
8943 fprintf (stderr, "Code Nodes\n");
8944 fprintf (stderr, "----------------------------\n");
8945 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8946 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8947 fprintf (stderr, "----------------------------\n");
8948 ssanames_print_statistics ();
8949 phinodes_print_statistics ();
8951 else
8952 fprintf (stderr, "(No per-node statistics)\n");
8954 print_type_hash_statistics ();
8955 print_debug_expr_statistics ();
8956 print_value_expr_statistics ();
8957 lang_hooks.print_statistics ();
8960 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8962 /* Generate a crc32 of a byte. */
8964 static unsigned
8965 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8967 unsigned ix;
8969 for (ix = bits; ix--; value <<= 1)
8971 unsigned feedback;
8973 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8974 chksum <<= 1;
8975 chksum ^= feedback;
8977 return chksum;
8980 /* Generate a crc32 of a 32-bit unsigned. */
8982 unsigned
8983 crc32_unsigned (unsigned chksum, unsigned value)
8985 return crc32_unsigned_bits (chksum, value, 32);
8988 /* Generate a crc32 of a byte. */
8990 unsigned
8991 crc32_byte (unsigned chksum, char byte)
8993 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8996 /* Generate a crc32 of a string. */
8998 unsigned
8999 crc32_string (unsigned chksum, const char *string)
9003 chksum = crc32_byte (chksum, *string);
9005 while (*string++);
9006 return chksum;
9009 /* P is a string that will be used in a symbol. Mask out any characters
9010 that are not valid in that context. */
9012 void
9013 clean_symbol_name (char *p)
9015 for (; *p; p++)
9016 if (! (ISALNUM (*p)
9017 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9018 || *p == '$'
9019 #endif
9020 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9021 || *p == '.'
9022 #endif
9024 *p = '_';
9027 /* Generate a name for a special-purpose function.
9028 The generated name may need to be unique across the whole link.
9029 Changes to this function may also require corresponding changes to
9030 xstrdup_mask_random.
9031 TYPE is some string to identify the purpose of this function to the
9032 linker or collect2; it must start with an uppercase letter,
9033 one of:
9034 I - for constructors
9035 D - for destructors
9036 N - for C++ anonymous namespaces
9037 F - for DWARF unwind frame information. */
9039 tree
9040 get_file_function_name (const char *type)
9042 char *buf;
9043 const char *p;
9044 char *q;
9046 /* If we already have a name we know to be unique, just use that. */
9047 if (first_global_object_name)
9048 p = q = ASTRDUP (first_global_object_name);
9049 /* If the target is handling the constructors/destructors, they
9050 will be local to this file and the name is only necessary for
9051 debugging purposes.
9052 We also assign sub_I and sub_D sufixes to constructors called from
9053 the global static constructors. These are always local. */
9054 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9055 || (strncmp (type, "sub_", 4) == 0
9056 && (type[4] == 'I' || type[4] == 'D')))
9058 const char *file = main_input_filename;
9059 if (! file)
9060 file = input_filename;
9061 /* Just use the file's basename, because the full pathname
9062 might be quite long. */
9063 p = q = ASTRDUP (lbasename (file));
9065 else
9067 /* Otherwise, the name must be unique across the entire link.
9068 We don't have anything that we know to be unique to this translation
9069 unit, so use what we do have and throw in some randomness. */
9070 unsigned len;
9071 const char *name = weak_global_object_name;
9072 const char *file = main_input_filename;
9074 if (! name)
9075 name = "";
9076 if (! file)
9077 file = input_filename;
9079 len = strlen (file);
9080 q = (char *) alloca (9 + 17 + len + 1);
9081 memcpy (q, file, len + 1);
9083 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9084 crc32_string (0, name), get_random_seed (false));
9086 p = q;
9089 clean_symbol_name (q);
9090 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9091 + strlen (type));
9093 /* Set up the name of the file-level functions we may need.
9094 Use a global object (which is already required to be unique over
9095 the program) rather than the file name (which imposes extra
9096 constraints). */
9097 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9099 return get_identifier (buf);
9102 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9104 /* Complain that the tree code of NODE does not match the expected 0
9105 terminated list of trailing codes. The trailing code list can be
9106 empty, for a more vague error message. FILE, LINE, and FUNCTION
9107 are of the caller. */
9109 void
9110 tree_check_failed (const_tree node, const char *file,
9111 int line, const char *function, ...)
9113 va_list args;
9114 const char *buffer;
9115 unsigned length = 0;
9116 int code;
9118 va_start (args, function);
9119 while ((code = va_arg (args, int)))
9120 length += 4 + strlen (tree_code_name[code]);
9121 va_end (args);
9122 if (length)
9124 char *tmp;
9125 va_start (args, function);
9126 length += strlen ("expected ");
9127 buffer = tmp = (char *) alloca (length);
9128 length = 0;
9129 while ((code = va_arg (args, int)))
9131 const char *prefix = length ? " or " : "expected ";
9133 strcpy (tmp + length, prefix);
9134 length += strlen (prefix);
9135 strcpy (tmp + length, tree_code_name[code]);
9136 length += strlen (tree_code_name[code]);
9138 va_end (args);
9140 else
9141 buffer = "unexpected node";
9143 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9144 buffer, tree_code_name[TREE_CODE (node)],
9145 function, trim_filename (file), line);
9148 /* Complain that the tree code of NODE does match the expected 0
9149 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9150 the caller. */
9152 void
9153 tree_not_check_failed (const_tree node, const char *file,
9154 int line, const char *function, ...)
9156 va_list args;
9157 char *buffer;
9158 unsigned length = 0;
9159 int code;
9161 va_start (args, function);
9162 while ((code = va_arg (args, int)))
9163 length += 4 + strlen (tree_code_name[code]);
9164 va_end (args);
9165 va_start (args, function);
9166 buffer = (char *) alloca (length);
9167 length = 0;
9168 while ((code = va_arg (args, int)))
9170 if (length)
9172 strcpy (buffer + length, " or ");
9173 length += 4;
9175 strcpy (buffer + length, tree_code_name[code]);
9176 length += strlen (tree_code_name[code]);
9178 va_end (args);
9180 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9181 buffer, tree_code_name[TREE_CODE (node)],
9182 function, trim_filename (file), line);
9185 /* Similar to tree_check_failed, except that we check for a class of tree
9186 code, given in CL. */
9188 void
9189 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9190 const char *file, int line, const char *function)
9192 internal_error
9193 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9194 TREE_CODE_CLASS_STRING (cl),
9195 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9196 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9199 /* Similar to tree_check_failed, except that instead of specifying a
9200 dozen codes, use the knowledge that they're all sequential. */
9202 void
9203 tree_range_check_failed (const_tree node, const char *file, int line,
9204 const char *function, enum tree_code c1,
9205 enum tree_code c2)
9207 char *buffer;
9208 unsigned length = 0;
9209 unsigned int c;
9211 for (c = c1; c <= c2; ++c)
9212 length += 4 + strlen (tree_code_name[c]);
9214 length += strlen ("expected ");
9215 buffer = (char *) alloca (length);
9216 length = 0;
9218 for (c = c1; c <= c2; ++c)
9220 const char *prefix = length ? " or " : "expected ";
9222 strcpy (buffer + length, prefix);
9223 length += strlen (prefix);
9224 strcpy (buffer + length, tree_code_name[c]);
9225 length += strlen (tree_code_name[c]);
9228 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9229 buffer, tree_code_name[TREE_CODE (node)],
9230 function, trim_filename (file), line);
9234 /* Similar to tree_check_failed, except that we check that a tree does
9235 not have the specified code, given in CL. */
9237 void
9238 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9239 const char *file, int line, const char *function)
9241 internal_error
9242 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9243 TREE_CODE_CLASS_STRING (cl),
9244 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9245 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9249 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9251 void
9252 omp_clause_check_failed (const_tree node, const char *file, int line,
9253 const char *function, enum omp_clause_code code)
9255 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9256 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9257 function, trim_filename (file), line);
9261 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9263 void
9264 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9265 const char *function, enum omp_clause_code c1,
9266 enum omp_clause_code c2)
9268 char *buffer;
9269 unsigned length = 0;
9270 unsigned int c;
9272 for (c = c1; c <= c2; ++c)
9273 length += 4 + strlen (omp_clause_code_name[c]);
9275 length += strlen ("expected ");
9276 buffer = (char *) alloca (length);
9277 length = 0;
9279 for (c = c1; c <= c2; ++c)
9281 const char *prefix = length ? " or " : "expected ";
9283 strcpy (buffer + length, prefix);
9284 length += strlen (prefix);
9285 strcpy (buffer + length, omp_clause_code_name[c]);
9286 length += strlen (omp_clause_code_name[c]);
9289 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9290 buffer, omp_clause_code_name[TREE_CODE (node)],
9291 function, trim_filename (file), line);
9295 #undef DEFTREESTRUCT
9296 #define DEFTREESTRUCT(VAL, NAME) NAME,
9298 static const char *ts_enum_names[] = {
9299 #include "treestruct.def"
9301 #undef DEFTREESTRUCT
9303 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9305 /* Similar to tree_class_check_failed, except that we check for
9306 whether CODE contains the tree structure identified by EN. */
9308 void
9309 tree_contains_struct_check_failed (const_tree node,
9310 const enum tree_node_structure_enum en,
9311 const char *file, int line,
9312 const char *function)
9314 internal_error
9315 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9316 TS_ENUM_NAME(en),
9317 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9321 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9322 (dynamically sized) vector. */
9324 void
9325 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9326 const char *function)
9328 internal_error
9329 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9330 idx + 1, len, function, trim_filename (file), line);
9333 /* Similar to above, except that the check is for the bounds of the operand
9334 vector of an expression node EXP. */
9336 void
9337 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9338 int line, const char *function)
9340 int code = TREE_CODE (exp);
9341 internal_error
9342 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9343 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9344 function, trim_filename (file), line);
9347 /* Similar to above, except that the check is for the number of
9348 operands of an OMP_CLAUSE node. */
9350 void
9351 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9352 int line, const char *function)
9354 internal_error
9355 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9356 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9357 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9358 trim_filename (file), line);
9360 #endif /* ENABLE_TREE_CHECKING */
9362 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9363 and mapped to the machine mode MODE. Initialize its fields and build
9364 the information necessary for debugging output. */
9366 static tree
9367 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9369 tree t;
9370 hashval_t hashcode = 0;
9372 t = make_node (VECTOR_TYPE);
9373 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9374 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9375 SET_TYPE_MODE (t, mode);
9377 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9378 SET_TYPE_STRUCTURAL_EQUALITY (t);
9379 else if (TYPE_CANONICAL (innertype) != innertype
9380 || mode != VOIDmode)
9381 TYPE_CANONICAL (t)
9382 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9384 layout_type (t);
9386 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9387 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9388 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9389 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9390 t = type_hash_canon (hashcode, t);
9392 /* We have built a main variant, based on the main variant of the
9393 inner type. Use it to build the variant we return. */
9394 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9395 && TREE_TYPE (t) != innertype)
9396 return build_type_attribute_qual_variant (t,
9397 TYPE_ATTRIBUTES (innertype),
9398 TYPE_QUALS (innertype));
9400 return t;
9403 static tree
9404 make_or_reuse_type (unsigned size, int unsignedp)
9406 if (size == INT_TYPE_SIZE)
9407 return unsignedp ? unsigned_type_node : integer_type_node;
9408 if (size == CHAR_TYPE_SIZE)
9409 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9410 if (size == SHORT_TYPE_SIZE)
9411 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9412 if (size == LONG_TYPE_SIZE)
9413 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9414 if (size == LONG_LONG_TYPE_SIZE)
9415 return (unsignedp ? long_long_unsigned_type_node
9416 : long_long_integer_type_node);
9417 if (size == 128 && int128_integer_type_node)
9418 return (unsignedp ? int128_unsigned_type_node
9419 : int128_integer_type_node);
9421 if (unsignedp)
9422 return make_unsigned_type (size);
9423 else
9424 return make_signed_type (size);
9427 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9429 static tree
9430 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9432 if (satp)
9434 if (size == SHORT_FRACT_TYPE_SIZE)
9435 return unsignedp ? sat_unsigned_short_fract_type_node
9436 : sat_short_fract_type_node;
9437 if (size == FRACT_TYPE_SIZE)
9438 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9439 if (size == LONG_FRACT_TYPE_SIZE)
9440 return unsignedp ? sat_unsigned_long_fract_type_node
9441 : sat_long_fract_type_node;
9442 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9443 return unsignedp ? sat_unsigned_long_long_fract_type_node
9444 : sat_long_long_fract_type_node;
9446 else
9448 if (size == SHORT_FRACT_TYPE_SIZE)
9449 return unsignedp ? unsigned_short_fract_type_node
9450 : short_fract_type_node;
9451 if (size == FRACT_TYPE_SIZE)
9452 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9453 if (size == LONG_FRACT_TYPE_SIZE)
9454 return unsignedp ? unsigned_long_fract_type_node
9455 : long_fract_type_node;
9456 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9457 return unsignedp ? unsigned_long_long_fract_type_node
9458 : long_long_fract_type_node;
9461 return make_fract_type (size, unsignedp, satp);
9464 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9466 static tree
9467 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9469 if (satp)
9471 if (size == SHORT_ACCUM_TYPE_SIZE)
9472 return unsignedp ? sat_unsigned_short_accum_type_node
9473 : sat_short_accum_type_node;
9474 if (size == ACCUM_TYPE_SIZE)
9475 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9476 if (size == LONG_ACCUM_TYPE_SIZE)
9477 return unsignedp ? sat_unsigned_long_accum_type_node
9478 : sat_long_accum_type_node;
9479 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9480 return unsignedp ? sat_unsigned_long_long_accum_type_node
9481 : sat_long_long_accum_type_node;
9483 else
9485 if (size == SHORT_ACCUM_TYPE_SIZE)
9486 return unsignedp ? unsigned_short_accum_type_node
9487 : short_accum_type_node;
9488 if (size == ACCUM_TYPE_SIZE)
9489 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9490 if (size == LONG_ACCUM_TYPE_SIZE)
9491 return unsignedp ? unsigned_long_accum_type_node
9492 : long_accum_type_node;
9493 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9494 return unsignedp ? unsigned_long_long_accum_type_node
9495 : long_long_accum_type_node;
9498 return make_accum_type (size, unsignedp, satp);
9501 /* Create nodes for all integer types (and error_mark_node) using the sizes
9502 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9503 SHORT_DOUBLE specifies whether double should be of the same precision
9504 as float. */
9506 void
9507 build_common_tree_nodes (bool signed_char, bool short_double)
9509 error_mark_node = make_node (ERROR_MARK);
9510 TREE_TYPE (error_mark_node) = error_mark_node;
9512 initialize_sizetypes ();
9514 /* Define both `signed char' and `unsigned char'. */
9515 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9516 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9517 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9518 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9520 /* Define `char', which is like either `signed char' or `unsigned char'
9521 but not the same as either. */
9522 char_type_node
9523 = (signed_char
9524 ? make_signed_type (CHAR_TYPE_SIZE)
9525 : make_unsigned_type (CHAR_TYPE_SIZE));
9526 TYPE_STRING_FLAG (char_type_node) = 1;
9528 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9529 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9530 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9531 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9532 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9533 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9534 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9535 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9536 #if HOST_BITS_PER_WIDE_INT >= 64
9537 /* TODO: This isn't correct, but as logic depends at the moment on
9538 host's instead of target's wide-integer.
9539 If there is a target not supporting TImode, but has an 128-bit
9540 integer-scalar register, this target check needs to be adjusted. */
9541 if (targetm.scalar_mode_supported_p (TImode))
9543 int128_integer_type_node = make_signed_type (128);
9544 int128_unsigned_type_node = make_unsigned_type (128);
9546 #endif
9548 /* Define a boolean type. This type only represents boolean values but
9549 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9550 Front ends which want to override this size (i.e. Java) can redefine
9551 boolean_type_node before calling build_common_tree_nodes_2. */
9552 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9553 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9554 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9555 TYPE_PRECISION (boolean_type_node) = 1;
9557 /* Define what type to use for size_t. */
9558 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9559 size_type_node = unsigned_type_node;
9560 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9561 size_type_node = long_unsigned_type_node;
9562 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9563 size_type_node = long_long_unsigned_type_node;
9564 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9565 size_type_node = short_unsigned_type_node;
9566 else
9567 gcc_unreachable ();
9569 /* Fill in the rest of the sized types. Reuse existing type nodes
9570 when possible. */
9571 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9572 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9573 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9574 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9575 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9577 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9578 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9579 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9580 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9581 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9583 access_public_node = get_identifier ("public");
9584 access_protected_node = get_identifier ("protected");
9585 access_private_node = get_identifier ("private");
9587 /* Define these next since types below may used them. */
9588 integer_zero_node = build_int_cst (integer_type_node, 0);
9589 integer_one_node = build_int_cst (integer_type_node, 1);
9590 integer_three_node = build_int_cst (integer_type_node, 3);
9591 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9593 size_zero_node = size_int (0);
9594 size_one_node = size_int (1);
9595 bitsize_zero_node = bitsize_int (0);
9596 bitsize_one_node = bitsize_int (1);
9597 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9599 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9600 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9602 void_type_node = make_node (VOID_TYPE);
9603 layout_type (void_type_node);
9605 /* We are not going to have real types in C with less than byte alignment,
9606 so we might as well not have any types that claim to have it. */
9607 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9608 TYPE_USER_ALIGN (void_type_node) = 0;
9610 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9611 layout_type (TREE_TYPE (null_pointer_node));
9613 ptr_type_node = build_pointer_type (void_type_node);
9614 const_ptr_type_node
9615 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9616 fileptr_type_node = ptr_type_node;
9618 float_type_node = make_node (REAL_TYPE);
9619 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9620 layout_type (float_type_node);
9622 double_type_node = make_node (REAL_TYPE);
9623 if (short_double)
9624 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9625 else
9626 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9627 layout_type (double_type_node);
9629 long_double_type_node = make_node (REAL_TYPE);
9630 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9631 layout_type (long_double_type_node);
9633 float_ptr_type_node = build_pointer_type (float_type_node);
9634 double_ptr_type_node = build_pointer_type (double_type_node);
9635 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9636 integer_ptr_type_node = build_pointer_type (integer_type_node);
9638 /* Fixed size integer types. */
9639 uint16_type_node = build_nonstandard_integer_type (16, true);
9640 uint32_type_node = build_nonstandard_integer_type (32, true);
9641 uint64_type_node = build_nonstandard_integer_type (64, true);
9643 /* Decimal float types. */
9644 dfloat32_type_node = make_node (REAL_TYPE);
9645 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9646 layout_type (dfloat32_type_node);
9647 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9648 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9650 dfloat64_type_node = make_node (REAL_TYPE);
9651 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9652 layout_type (dfloat64_type_node);
9653 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9654 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9656 dfloat128_type_node = make_node (REAL_TYPE);
9657 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9658 layout_type (dfloat128_type_node);
9659 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9660 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9662 complex_integer_type_node = build_complex_type (integer_type_node);
9663 complex_float_type_node = build_complex_type (float_type_node);
9664 complex_double_type_node = build_complex_type (double_type_node);
9665 complex_long_double_type_node = build_complex_type (long_double_type_node);
9667 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9668 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9669 sat_ ## KIND ## _type_node = \
9670 make_sat_signed_ ## KIND ## _type (SIZE); \
9671 sat_unsigned_ ## KIND ## _type_node = \
9672 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9673 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9674 unsigned_ ## KIND ## _type_node = \
9675 make_unsigned_ ## KIND ## _type (SIZE);
9677 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9678 sat_ ## WIDTH ## KIND ## _type_node = \
9679 make_sat_signed_ ## KIND ## _type (SIZE); \
9680 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9681 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9682 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9683 unsigned_ ## WIDTH ## KIND ## _type_node = \
9684 make_unsigned_ ## KIND ## _type (SIZE);
9686 /* Make fixed-point type nodes based on four different widths. */
9687 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9688 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9689 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9690 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9691 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9693 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9694 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9695 NAME ## _type_node = \
9696 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9697 u ## NAME ## _type_node = \
9698 make_or_reuse_unsigned_ ## KIND ## _type \
9699 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9700 sat_ ## NAME ## _type_node = \
9701 make_or_reuse_sat_signed_ ## KIND ## _type \
9702 (GET_MODE_BITSIZE (MODE ## mode)); \
9703 sat_u ## NAME ## _type_node = \
9704 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9705 (GET_MODE_BITSIZE (U ## MODE ## mode));
9707 /* Fixed-point type and mode nodes. */
9708 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9709 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9710 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9711 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9712 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9713 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9714 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9715 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9716 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9717 MAKE_FIXED_MODE_NODE (accum, da, DA)
9718 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9721 tree t = targetm.build_builtin_va_list ();
9723 /* Many back-ends define record types without setting TYPE_NAME.
9724 If we copied the record type here, we'd keep the original
9725 record type without a name. This breaks name mangling. So,
9726 don't copy record types and let c_common_nodes_and_builtins()
9727 declare the type to be __builtin_va_list. */
9728 if (TREE_CODE (t) != RECORD_TYPE)
9729 t = build_variant_type_copy (t);
9731 va_list_type_node = t;
9735 /* Modify DECL for given flags. */
9736 void
9737 set_call_expr_flags (tree decl, int flags)
9739 if (flags & ECF_NOTHROW)
9740 TREE_NOTHROW (decl) = 1;
9741 if (flags & ECF_CONST)
9742 TREE_READONLY (decl) = 1;
9743 if (flags & ECF_PURE)
9744 DECL_PURE_P (decl) = 1;
9745 if (flags & ECF_LOOPING_CONST_OR_PURE)
9746 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9747 if (flags & ECF_NOVOPS)
9748 DECL_IS_NOVOPS (decl) = 1;
9749 if (flags & ECF_NORETURN)
9750 TREE_THIS_VOLATILE (decl) = 1;
9751 if (flags & ECF_MALLOC)
9752 DECL_IS_MALLOC (decl) = 1;
9753 if (flags & ECF_RETURNS_TWICE)
9754 DECL_IS_RETURNS_TWICE (decl) = 1;
9755 if (flags & ECF_LEAF)
9756 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9757 NULL, DECL_ATTRIBUTES (decl));
9758 if ((flags & ECF_TM_PURE) && flag_tm)
9759 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9760 NULL, DECL_ATTRIBUTES (decl));
9761 /* Looping const or pure is implied by noreturn.
9762 There is currently no way to declare looping const or looping pure alone. */
9763 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9764 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9768 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9770 static void
9771 local_define_builtin (const char *name, tree type, enum built_in_function code,
9772 const char *library_name, int ecf_flags)
9774 tree decl;
9776 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9777 library_name, NULL_TREE);
9778 set_call_expr_flags (decl, ecf_flags);
9780 set_builtin_decl (code, decl, true);
9783 /* Call this function after instantiating all builtins that the language
9784 front end cares about. This will build the rest of the builtins that
9785 are relied upon by the tree optimizers and the middle-end. */
9787 void
9788 build_common_builtin_nodes (void)
9790 tree tmp, ftype;
9791 int ecf_flags;
9793 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9795 ftype = build_function_type (void_type_node, void_list_node);
9796 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9797 "__builtin_unreachable",
9798 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9799 | ECF_CONST | ECF_LEAF);
9802 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9803 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9805 ftype = build_function_type_list (ptr_type_node,
9806 ptr_type_node, const_ptr_type_node,
9807 size_type_node, NULL_TREE);
9809 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9810 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9811 "memcpy", ECF_NOTHROW | ECF_LEAF);
9812 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9813 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9814 "memmove", ECF_NOTHROW | ECF_LEAF);
9817 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9819 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9820 const_ptr_type_node, size_type_node,
9821 NULL_TREE);
9822 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9823 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9826 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9828 ftype = build_function_type_list (ptr_type_node,
9829 ptr_type_node, integer_type_node,
9830 size_type_node, NULL_TREE);
9831 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9832 "memset", ECF_NOTHROW | ECF_LEAF);
9835 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9837 ftype = build_function_type_list (ptr_type_node,
9838 size_type_node, NULL_TREE);
9839 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9840 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9843 ftype = build_function_type_list (ptr_type_node, size_type_node,
9844 size_type_node, NULL_TREE);
9845 local_define_builtin ("__builtin_alloca_with_align", ftype,
9846 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9847 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9849 /* If we're checking the stack, `alloca' can throw. */
9850 if (flag_stack_check)
9852 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9853 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9856 ftype = build_function_type_list (void_type_node,
9857 ptr_type_node, ptr_type_node,
9858 ptr_type_node, NULL_TREE);
9859 local_define_builtin ("__builtin_init_trampoline", ftype,
9860 BUILT_IN_INIT_TRAMPOLINE,
9861 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9862 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9863 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9864 "__builtin_init_heap_trampoline",
9865 ECF_NOTHROW | ECF_LEAF);
9867 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9868 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9869 BUILT_IN_ADJUST_TRAMPOLINE,
9870 "__builtin_adjust_trampoline",
9871 ECF_CONST | ECF_NOTHROW);
9873 ftype = build_function_type_list (void_type_node,
9874 ptr_type_node, ptr_type_node, NULL_TREE);
9875 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9876 BUILT_IN_NONLOCAL_GOTO,
9877 "__builtin_nonlocal_goto",
9878 ECF_NORETURN | ECF_NOTHROW);
9880 ftype = build_function_type_list (void_type_node,
9881 ptr_type_node, ptr_type_node, NULL_TREE);
9882 local_define_builtin ("__builtin_setjmp_setup", ftype,
9883 BUILT_IN_SETJMP_SETUP,
9884 "__builtin_setjmp_setup", ECF_NOTHROW);
9886 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9887 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9888 BUILT_IN_SETJMP_DISPATCHER,
9889 "__builtin_setjmp_dispatcher",
9890 ECF_PURE | ECF_NOTHROW);
9892 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9893 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9894 BUILT_IN_SETJMP_RECEIVER,
9895 "__builtin_setjmp_receiver", ECF_NOTHROW);
9897 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9898 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9899 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9901 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9902 local_define_builtin ("__builtin_stack_restore", ftype,
9903 BUILT_IN_STACK_RESTORE,
9904 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9906 /* If there's a possibility that we might use the ARM EABI, build the
9907 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9908 if (targetm.arm_eabi_unwinder)
9910 ftype = build_function_type_list (void_type_node, NULL_TREE);
9911 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9912 BUILT_IN_CXA_END_CLEANUP,
9913 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9916 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9917 local_define_builtin ("__builtin_unwind_resume", ftype,
9918 BUILT_IN_UNWIND_RESUME,
9919 ((targetm_common.except_unwind_info (&global_options)
9920 == UI_SJLJ)
9921 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9922 ECF_NORETURN);
9924 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9926 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9927 NULL_TREE);
9928 local_define_builtin ("__builtin_return_address", ftype,
9929 BUILT_IN_RETURN_ADDRESS,
9930 "__builtin_return_address",
9931 ECF_NOTHROW);
9934 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9935 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9937 ftype = build_function_type_list (void_type_node, ptr_type_node,
9938 ptr_type_node, NULL_TREE);
9939 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9940 local_define_builtin ("__cyg_profile_func_enter", ftype,
9941 BUILT_IN_PROFILE_FUNC_ENTER,
9942 "__cyg_profile_func_enter", 0);
9943 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9944 local_define_builtin ("__cyg_profile_func_exit", ftype,
9945 BUILT_IN_PROFILE_FUNC_EXIT,
9946 "__cyg_profile_func_exit", 0);
9949 /* The exception object and filter values from the runtime. The argument
9950 must be zero before exception lowering, i.e. from the front end. After
9951 exception lowering, it will be the region number for the exception
9952 landing pad. These functions are PURE instead of CONST to prevent
9953 them from being hoisted past the exception edge that will initialize
9954 its value in the landing pad. */
9955 ftype = build_function_type_list (ptr_type_node,
9956 integer_type_node, NULL_TREE);
9957 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9958 /* Only use TM_PURE if we we have TM language support. */
9959 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9960 ecf_flags |= ECF_TM_PURE;
9961 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9962 "__builtin_eh_pointer", ecf_flags);
9964 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9965 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9966 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9967 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9969 ftype = build_function_type_list (void_type_node,
9970 integer_type_node, integer_type_node,
9971 NULL_TREE);
9972 local_define_builtin ("__builtin_eh_copy_values", ftype,
9973 BUILT_IN_EH_COPY_VALUES,
9974 "__builtin_eh_copy_values", ECF_NOTHROW);
9976 /* Complex multiplication and division. These are handled as builtins
9977 rather than optabs because emit_library_call_value doesn't support
9978 complex. Further, we can do slightly better with folding these
9979 beasties if the real and complex parts of the arguments are separate. */
9981 int mode;
9983 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9985 char mode_name_buf[4], *q;
9986 const char *p;
9987 enum built_in_function mcode, dcode;
9988 tree type, inner_type;
9989 const char *prefix = "__";
9991 if (targetm.libfunc_gnu_prefix)
9992 prefix = "__gnu_";
9994 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9995 if (type == NULL)
9996 continue;
9997 inner_type = TREE_TYPE (type);
9999 ftype = build_function_type_list (type, inner_type, inner_type,
10000 inner_type, inner_type, NULL_TREE);
10002 mcode = ((enum built_in_function)
10003 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10004 dcode = ((enum built_in_function)
10005 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10007 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10008 *q = TOLOWER (*p);
10009 *q = '\0';
10011 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10012 NULL);
10013 local_define_builtin (built_in_names[mcode], ftype, mcode,
10014 built_in_names[mcode],
10015 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10017 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10018 NULL);
10019 local_define_builtin (built_in_names[dcode], ftype, dcode,
10020 built_in_names[dcode],
10021 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10026 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10027 better way.
10029 If we requested a pointer to a vector, build up the pointers that
10030 we stripped off while looking for the inner type. Similarly for
10031 return values from functions.
10033 The argument TYPE is the top of the chain, and BOTTOM is the
10034 new type which we will point to. */
10036 tree
10037 reconstruct_complex_type (tree type, tree bottom)
10039 tree inner, outer;
10041 if (TREE_CODE (type) == POINTER_TYPE)
10043 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10044 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10045 TYPE_REF_CAN_ALIAS_ALL (type));
10047 else if (TREE_CODE (type) == REFERENCE_TYPE)
10049 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10050 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10051 TYPE_REF_CAN_ALIAS_ALL (type));
10053 else if (TREE_CODE (type) == ARRAY_TYPE)
10055 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10056 outer = build_array_type (inner, TYPE_DOMAIN (type));
10058 else if (TREE_CODE (type) == FUNCTION_TYPE)
10060 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10061 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10063 else if (TREE_CODE (type) == METHOD_TYPE)
10065 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10066 /* The build_method_type_directly() routine prepends 'this' to argument list,
10067 so we must compensate by getting rid of it. */
10068 outer
10069 = build_method_type_directly
10070 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10071 inner,
10072 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10074 else if (TREE_CODE (type) == OFFSET_TYPE)
10076 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10077 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10079 else
10080 return bottom;
10082 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10083 TYPE_QUALS (type));
10086 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10087 the inner type. */
10088 tree
10089 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10091 int nunits;
10093 switch (GET_MODE_CLASS (mode))
10095 case MODE_VECTOR_INT:
10096 case MODE_VECTOR_FLOAT:
10097 case MODE_VECTOR_FRACT:
10098 case MODE_VECTOR_UFRACT:
10099 case MODE_VECTOR_ACCUM:
10100 case MODE_VECTOR_UACCUM:
10101 nunits = GET_MODE_NUNITS (mode);
10102 break;
10104 case MODE_INT:
10105 /* Check that there are no leftover bits. */
10106 gcc_assert (GET_MODE_BITSIZE (mode)
10107 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10109 nunits = GET_MODE_BITSIZE (mode)
10110 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10111 break;
10113 default:
10114 gcc_unreachable ();
10117 return make_vector_type (innertype, nunits, mode);
10120 /* Similarly, but takes the inner type and number of units, which must be
10121 a power of two. */
10123 tree
10124 build_vector_type (tree innertype, int nunits)
10126 return make_vector_type (innertype, nunits, VOIDmode);
10129 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10131 tree
10132 build_opaque_vector_type (tree innertype, int nunits)
10134 tree t = make_vector_type (innertype, nunits, VOIDmode);
10135 tree cand;
10136 /* We always build the non-opaque variant before the opaque one,
10137 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10138 cand = TYPE_NEXT_VARIANT (t);
10139 if (cand
10140 && TYPE_VECTOR_OPAQUE (cand)
10141 && check_qualified_type (cand, t, TYPE_QUALS (t), NULL_TREE))
10142 return cand;
10143 /* Othewise build a variant type and make sure to queue it after
10144 the non-opaque type. */
10145 cand = build_distinct_type_copy (t);
10146 TYPE_VECTOR_OPAQUE (cand) = true;
10147 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10148 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10149 TYPE_NEXT_VARIANT (t) = cand;
10150 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10151 return cand;
10155 /* Given an initializer INIT, return TRUE if INIT is zero or some
10156 aggregate of zeros. Otherwise return FALSE. */
10157 bool
10158 initializer_zerop (const_tree init)
10160 tree elt;
10162 STRIP_NOPS (init);
10164 switch (TREE_CODE (init))
10166 case INTEGER_CST:
10167 return integer_zerop (init);
10169 case REAL_CST:
10170 /* ??? Note that this is not correct for C4X float formats. There,
10171 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10172 negative exponent. */
10173 return real_zerop (init)
10174 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10176 case FIXED_CST:
10177 return fixed_zerop (init);
10179 case COMPLEX_CST:
10180 return integer_zerop (init)
10181 || (real_zerop (init)
10182 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10183 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10185 case VECTOR_CST:
10187 unsigned i;
10188 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10189 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10190 return false;
10191 return true;
10194 case CONSTRUCTOR:
10196 unsigned HOST_WIDE_INT idx;
10198 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10199 if (!initializer_zerop (elt))
10200 return false;
10201 return true;
10204 case STRING_CST:
10206 int i;
10208 /* We need to loop through all elements to handle cases like
10209 "\0" and "\0foobar". */
10210 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10211 if (TREE_STRING_POINTER (init)[i] != '\0')
10212 return false;
10214 return true;
10217 default:
10218 return false;
10222 /* Build an empty statement at location LOC. */
10224 tree
10225 build_empty_stmt (location_t loc)
10227 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10228 SET_EXPR_LOCATION (t, loc);
10229 return t;
10233 /* Build an OpenMP clause with code CODE. LOC is the location of the
10234 clause. */
10236 tree
10237 build_omp_clause (location_t loc, enum omp_clause_code code)
10239 tree t;
10240 int size, length;
10242 length = omp_clause_num_ops[code];
10243 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10245 record_node_allocation_statistics (OMP_CLAUSE, size);
10247 t = ggc_alloc_tree_node (size);
10248 memset (t, 0, size);
10249 TREE_SET_CODE (t, OMP_CLAUSE);
10250 OMP_CLAUSE_SET_CODE (t, code);
10251 OMP_CLAUSE_LOCATION (t) = loc;
10253 return t;
10256 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10257 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10258 Except for the CODE and operand count field, other storage for the
10259 object is initialized to zeros. */
10261 tree
10262 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10264 tree t;
10265 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10267 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10268 gcc_assert (len >= 1);
10270 record_node_allocation_statistics (code, length);
10272 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10274 TREE_SET_CODE (t, code);
10276 /* Can't use TREE_OPERAND to store the length because if checking is
10277 enabled, it will try to check the length before we store it. :-P */
10278 t->exp.operands[0] = build_int_cst (sizetype, len);
10280 return t;
10283 /* Helper function for build_call_* functions; build a CALL_EXPR with
10284 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10285 the argument slots. */
10287 static tree
10288 build_call_1 (tree return_type, tree fn, int nargs)
10290 tree t;
10292 t = build_vl_exp (CALL_EXPR, nargs + 3);
10293 TREE_TYPE (t) = return_type;
10294 CALL_EXPR_FN (t) = fn;
10295 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10297 return t;
10300 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10301 FN and a null static chain slot. NARGS is the number of call arguments
10302 which are specified as "..." arguments. */
10304 tree
10305 build_call_nary (tree return_type, tree fn, int nargs, ...)
10307 tree ret;
10308 va_list args;
10309 va_start (args, nargs);
10310 ret = build_call_valist (return_type, fn, nargs, args);
10311 va_end (args);
10312 return ret;
10315 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10316 FN and a null static chain slot. NARGS is the number of call arguments
10317 which are specified as a va_list ARGS. */
10319 tree
10320 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10322 tree t;
10323 int i;
10325 t = build_call_1 (return_type, fn, nargs);
10326 for (i = 0; i < nargs; i++)
10327 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10328 process_call_operands (t);
10329 return t;
10332 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10333 FN and a null static chain slot. NARGS is the number of call arguments
10334 which are specified as a tree array ARGS. */
10336 tree
10337 build_call_array_loc (location_t loc, tree return_type, tree fn,
10338 int nargs, const tree *args)
10340 tree t;
10341 int i;
10343 t = build_call_1 (return_type, fn, nargs);
10344 for (i = 0; i < nargs; i++)
10345 CALL_EXPR_ARG (t, i) = args[i];
10346 process_call_operands (t);
10347 SET_EXPR_LOCATION (t, loc);
10348 return t;
10351 /* Like build_call_array, but takes a vec. */
10353 tree
10354 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10356 tree ret, t;
10357 unsigned int ix;
10359 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10360 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10361 CALL_EXPR_ARG (ret, ix) = t;
10362 process_call_operands (ret);
10363 return ret;
10367 /* Returns true if it is possible to prove that the index of
10368 an array access REF (an ARRAY_REF expression) falls into the
10369 array bounds. */
10371 bool
10372 in_array_bounds_p (tree ref)
10374 tree idx = TREE_OPERAND (ref, 1);
10375 tree min, max;
10377 if (TREE_CODE (idx) != INTEGER_CST)
10378 return false;
10380 min = array_ref_low_bound (ref);
10381 max = array_ref_up_bound (ref);
10382 if (!min
10383 || !max
10384 || TREE_CODE (min) != INTEGER_CST
10385 || TREE_CODE (max) != INTEGER_CST)
10386 return false;
10388 if (tree_int_cst_lt (idx, min)
10389 || tree_int_cst_lt (max, idx))
10390 return false;
10392 return true;
10395 /* Returns true if it is possible to prove that the range of
10396 an array access REF (an ARRAY_RANGE_REF expression) falls
10397 into the array bounds. */
10399 bool
10400 range_in_array_bounds_p (tree ref)
10402 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10403 tree range_min, range_max, min, max;
10405 range_min = TYPE_MIN_VALUE (domain_type);
10406 range_max = TYPE_MAX_VALUE (domain_type);
10407 if (!range_min
10408 || !range_max
10409 || TREE_CODE (range_min) != INTEGER_CST
10410 || TREE_CODE (range_max) != INTEGER_CST)
10411 return false;
10413 min = array_ref_low_bound (ref);
10414 max = array_ref_up_bound (ref);
10415 if (!min
10416 || !max
10417 || TREE_CODE (min) != INTEGER_CST
10418 || TREE_CODE (max) != INTEGER_CST)
10419 return false;
10421 if (tree_int_cst_lt (range_min, min)
10422 || tree_int_cst_lt (max, range_max))
10423 return false;
10425 return true;
10428 /* Return true if T (assumed to be a DECL) must be assigned a memory
10429 location. */
10431 bool
10432 needs_to_live_in_memory (const_tree t)
10434 return (TREE_ADDRESSABLE (t)
10435 || is_global_var (t)
10436 || (TREE_CODE (t) == RESULT_DECL
10437 && !DECL_BY_REFERENCE (t)
10438 && aggregate_value_p (t, current_function_decl)));
10441 /* Return value of a constant X and sign-extend it. */
10443 HOST_WIDE_INT
10444 int_cst_value (const_tree x)
10446 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10447 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10449 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10450 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10451 || TREE_INT_CST_HIGH (x) == -1);
10453 if (bits < HOST_BITS_PER_WIDE_INT)
10455 bool negative = ((val >> (bits - 1)) & 1) != 0;
10456 if (negative)
10457 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10458 else
10459 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10462 return val;
10465 /* Return value of a constant X and sign-extend it. */
10467 HOST_WIDEST_INT
10468 widest_int_cst_value (const_tree x)
10470 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10471 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10473 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10474 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10475 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10476 << HOST_BITS_PER_WIDE_INT);
10477 #else
10478 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10479 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10480 || TREE_INT_CST_HIGH (x) == -1);
10481 #endif
10483 if (bits < HOST_BITS_PER_WIDEST_INT)
10485 bool negative = ((val >> (bits - 1)) & 1) != 0;
10486 if (negative)
10487 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10488 else
10489 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10492 return val;
10495 /* If TYPE is an integral or pointer type, return an integer type with
10496 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10497 if TYPE is already an integer type of signedness UNSIGNEDP. */
10499 tree
10500 signed_or_unsigned_type_for (int unsignedp, tree type)
10502 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10503 return type;
10505 if (TREE_CODE (type) == VECTOR_TYPE)
10507 tree inner = TREE_TYPE (type);
10508 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10509 if (!inner2)
10510 return NULL_TREE;
10511 if (inner == inner2)
10512 return type;
10513 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10516 if (!INTEGRAL_TYPE_P (type)
10517 && !POINTER_TYPE_P (type))
10518 return NULL_TREE;
10520 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10523 /* If TYPE is an integral or pointer type, return an integer type with
10524 the same precision which is unsigned, or itself if TYPE is already an
10525 unsigned integer type. */
10527 tree
10528 unsigned_type_for (tree type)
10530 return signed_or_unsigned_type_for (1, type);
10533 /* If TYPE is an integral or pointer type, return an integer type with
10534 the same precision which is signed, or itself if TYPE is already a
10535 signed integer type. */
10537 tree
10538 signed_type_for (tree type)
10540 return signed_or_unsigned_type_for (0, type);
10543 /* If TYPE is a vector type, return a signed integer vector type with the
10544 same width and number of subparts. Otherwise return boolean_type_node. */
10546 tree
10547 truth_type_for (tree type)
10549 if (TREE_CODE (type) == VECTOR_TYPE)
10551 tree elem = lang_hooks.types.type_for_size
10552 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10553 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10555 else
10556 return boolean_type_node;
10559 /* Returns the largest value obtainable by casting something in INNER type to
10560 OUTER type. */
10562 tree
10563 upper_bound_in_type (tree outer, tree inner)
10565 double_int high;
10566 unsigned int det = 0;
10567 unsigned oprec = TYPE_PRECISION (outer);
10568 unsigned iprec = TYPE_PRECISION (inner);
10569 unsigned prec;
10571 /* Compute a unique number for every combination. */
10572 det |= (oprec > iprec) ? 4 : 0;
10573 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10574 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10576 /* Determine the exponent to use. */
10577 switch (det)
10579 case 0:
10580 case 1:
10581 /* oprec <= iprec, outer: signed, inner: don't care. */
10582 prec = oprec - 1;
10583 break;
10584 case 2:
10585 case 3:
10586 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10587 prec = oprec;
10588 break;
10589 case 4:
10590 /* oprec > iprec, outer: signed, inner: signed. */
10591 prec = iprec - 1;
10592 break;
10593 case 5:
10594 /* oprec > iprec, outer: signed, inner: unsigned. */
10595 prec = iprec;
10596 break;
10597 case 6:
10598 /* oprec > iprec, outer: unsigned, inner: signed. */
10599 prec = oprec;
10600 break;
10601 case 7:
10602 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10603 prec = iprec;
10604 break;
10605 default:
10606 gcc_unreachable ();
10609 /* Compute 2^^prec - 1. */
10610 if (prec <= HOST_BITS_PER_WIDE_INT)
10612 high.high = 0;
10613 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10614 >> (HOST_BITS_PER_WIDE_INT - prec));
10616 else
10618 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10619 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10620 high.low = ~(unsigned HOST_WIDE_INT) 0;
10623 return double_int_to_tree (outer, high);
10626 /* Returns the smallest value obtainable by casting something in INNER type to
10627 OUTER type. */
10629 tree
10630 lower_bound_in_type (tree outer, tree inner)
10632 double_int low;
10633 unsigned oprec = TYPE_PRECISION (outer);
10634 unsigned iprec = TYPE_PRECISION (inner);
10636 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10637 and obtain 0. */
10638 if (TYPE_UNSIGNED (outer)
10639 /* If we are widening something of an unsigned type, OUTER type
10640 contains all values of INNER type. In particular, both INNER
10641 and OUTER types have zero in common. */
10642 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10643 low.low = low.high = 0;
10644 else
10646 /* If we are widening a signed type to another signed type, we
10647 want to obtain -2^^(iprec-1). If we are keeping the
10648 precision or narrowing to a signed type, we want to obtain
10649 -2^(oprec-1). */
10650 unsigned prec = oprec > iprec ? iprec : oprec;
10652 if (prec <= HOST_BITS_PER_WIDE_INT)
10654 low.high = ~(unsigned HOST_WIDE_INT) 0;
10655 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10657 else
10659 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10660 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10661 low.low = 0;
10665 return double_int_to_tree (outer, low);
10668 /* Return nonzero if two operands that are suitable for PHI nodes are
10669 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10670 SSA_NAME or invariant. Note that this is strictly an optimization.
10671 That is, callers of this function can directly call operand_equal_p
10672 and get the same result, only slower. */
10675 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10677 if (arg0 == arg1)
10678 return 1;
10679 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10680 return 0;
10681 return operand_equal_p (arg0, arg1, 0);
10684 /* Returns number of zeros at the end of binary representation of X.
10686 ??? Use ffs if available? */
10688 tree
10689 num_ending_zeros (const_tree x)
10691 unsigned HOST_WIDE_INT fr, nfr;
10692 unsigned num, abits;
10693 tree type = TREE_TYPE (x);
10695 if (TREE_INT_CST_LOW (x) == 0)
10697 num = HOST_BITS_PER_WIDE_INT;
10698 fr = TREE_INT_CST_HIGH (x);
10700 else
10702 num = 0;
10703 fr = TREE_INT_CST_LOW (x);
10706 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10708 nfr = fr >> abits;
10709 if (nfr << abits == fr)
10711 num += abits;
10712 fr = nfr;
10716 if (num > TYPE_PRECISION (type))
10717 num = TYPE_PRECISION (type);
10719 return build_int_cst_type (type, num);
10723 #define WALK_SUBTREE(NODE) \
10724 do \
10726 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10727 if (result) \
10728 return result; \
10730 while (0)
10732 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10733 be walked whenever a type is seen in the tree. Rest of operands and return
10734 value are as for walk_tree. */
10736 static tree
10737 walk_type_fields (tree type, walk_tree_fn func, void *data,
10738 struct pointer_set_t *pset, walk_tree_lh lh)
10740 tree result = NULL_TREE;
10742 switch (TREE_CODE (type))
10744 case POINTER_TYPE:
10745 case REFERENCE_TYPE:
10746 /* We have to worry about mutually recursive pointers. These can't
10747 be written in C. They can in Ada. It's pathological, but
10748 there's an ACATS test (c38102a) that checks it. Deal with this
10749 by checking if we're pointing to another pointer, that one
10750 points to another pointer, that one does too, and we have no htab.
10751 If so, get a hash table. We check three levels deep to avoid
10752 the cost of the hash table if we don't need one. */
10753 if (POINTER_TYPE_P (TREE_TYPE (type))
10754 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10755 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10756 && !pset)
10758 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10759 func, data);
10760 if (result)
10761 return result;
10763 break;
10766 /* ... fall through ... */
10768 case COMPLEX_TYPE:
10769 WALK_SUBTREE (TREE_TYPE (type));
10770 break;
10772 case METHOD_TYPE:
10773 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10775 /* Fall through. */
10777 case FUNCTION_TYPE:
10778 WALK_SUBTREE (TREE_TYPE (type));
10780 tree arg;
10782 /* We never want to walk into default arguments. */
10783 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10784 WALK_SUBTREE (TREE_VALUE (arg));
10786 break;
10788 case ARRAY_TYPE:
10789 /* Don't follow this nodes's type if a pointer for fear that
10790 we'll have infinite recursion. If we have a PSET, then we
10791 need not fear. */
10792 if (pset
10793 || (!POINTER_TYPE_P (TREE_TYPE (type))
10794 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10795 WALK_SUBTREE (TREE_TYPE (type));
10796 WALK_SUBTREE (TYPE_DOMAIN (type));
10797 break;
10799 case OFFSET_TYPE:
10800 WALK_SUBTREE (TREE_TYPE (type));
10801 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10802 break;
10804 default:
10805 break;
10808 return NULL_TREE;
10811 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10812 called with the DATA and the address of each sub-tree. If FUNC returns a
10813 non-NULL value, the traversal is stopped, and the value returned by FUNC
10814 is returned. If PSET is non-NULL it is used to record the nodes visited,
10815 and to avoid visiting a node more than once. */
10817 tree
10818 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10819 struct pointer_set_t *pset, walk_tree_lh lh)
10821 enum tree_code code;
10822 int walk_subtrees;
10823 tree result;
10825 #define WALK_SUBTREE_TAIL(NODE) \
10826 do \
10828 tp = & (NODE); \
10829 goto tail_recurse; \
10831 while (0)
10833 tail_recurse:
10834 /* Skip empty subtrees. */
10835 if (!*tp)
10836 return NULL_TREE;
10838 /* Don't walk the same tree twice, if the user has requested
10839 that we avoid doing so. */
10840 if (pset && pointer_set_insert (pset, *tp))
10841 return NULL_TREE;
10843 /* Call the function. */
10844 walk_subtrees = 1;
10845 result = (*func) (tp, &walk_subtrees, data);
10847 /* If we found something, return it. */
10848 if (result)
10849 return result;
10851 code = TREE_CODE (*tp);
10853 /* Even if we didn't, FUNC may have decided that there was nothing
10854 interesting below this point in the tree. */
10855 if (!walk_subtrees)
10857 /* But we still need to check our siblings. */
10858 if (code == TREE_LIST)
10859 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10860 else if (code == OMP_CLAUSE)
10861 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10862 else
10863 return NULL_TREE;
10866 if (lh)
10868 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10869 if (result || !walk_subtrees)
10870 return result;
10873 switch (code)
10875 case ERROR_MARK:
10876 case IDENTIFIER_NODE:
10877 case INTEGER_CST:
10878 case REAL_CST:
10879 case FIXED_CST:
10880 case VECTOR_CST:
10881 case STRING_CST:
10882 case BLOCK:
10883 case PLACEHOLDER_EXPR:
10884 case SSA_NAME:
10885 case FIELD_DECL:
10886 case RESULT_DECL:
10887 /* None of these have subtrees other than those already walked
10888 above. */
10889 break;
10891 case TREE_LIST:
10892 WALK_SUBTREE (TREE_VALUE (*tp));
10893 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10894 break;
10896 case TREE_VEC:
10898 int len = TREE_VEC_LENGTH (*tp);
10900 if (len == 0)
10901 break;
10903 /* Walk all elements but the first. */
10904 while (--len)
10905 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10907 /* Now walk the first one as a tail call. */
10908 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10911 case COMPLEX_CST:
10912 WALK_SUBTREE (TREE_REALPART (*tp));
10913 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10915 case CONSTRUCTOR:
10917 unsigned HOST_WIDE_INT idx;
10918 constructor_elt *ce;
10920 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10921 WALK_SUBTREE (ce->value);
10923 break;
10925 case SAVE_EXPR:
10926 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10928 case BIND_EXPR:
10930 tree decl;
10931 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10933 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10934 into declarations that are just mentioned, rather than
10935 declared; they don't really belong to this part of the tree.
10936 And, we can see cycles: the initializer for a declaration
10937 can refer to the declaration itself. */
10938 WALK_SUBTREE (DECL_INITIAL (decl));
10939 WALK_SUBTREE (DECL_SIZE (decl));
10940 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10942 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10945 case STATEMENT_LIST:
10947 tree_stmt_iterator i;
10948 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10949 WALK_SUBTREE (*tsi_stmt_ptr (i));
10951 break;
10953 case OMP_CLAUSE:
10954 switch (OMP_CLAUSE_CODE (*tp))
10956 case OMP_CLAUSE_PRIVATE:
10957 case OMP_CLAUSE_SHARED:
10958 case OMP_CLAUSE_FIRSTPRIVATE:
10959 case OMP_CLAUSE_COPYIN:
10960 case OMP_CLAUSE_COPYPRIVATE:
10961 case OMP_CLAUSE_FINAL:
10962 case OMP_CLAUSE_IF:
10963 case OMP_CLAUSE_NUM_THREADS:
10964 case OMP_CLAUSE_SCHEDULE:
10965 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10966 /* FALLTHRU */
10968 case OMP_CLAUSE_NOWAIT:
10969 case OMP_CLAUSE_ORDERED:
10970 case OMP_CLAUSE_DEFAULT:
10971 case OMP_CLAUSE_UNTIED:
10972 case OMP_CLAUSE_MERGEABLE:
10973 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10975 case OMP_CLAUSE_LASTPRIVATE:
10976 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10977 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10978 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10980 case OMP_CLAUSE_COLLAPSE:
10982 int i;
10983 for (i = 0; i < 3; i++)
10984 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10985 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10988 case OMP_CLAUSE_REDUCTION:
10990 int i;
10991 for (i = 0; i < 4; i++)
10992 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10993 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10996 default:
10997 gcc_unreachable ();
10999 break;
11001 case TARGET_EXPR:
11003 int i, len;
11005 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11006 But, we only want to walk once. */
11007 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11008 for (i = 0; i < len; ++i)
11009 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11010 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11013 case DECL_EXPR:
11014 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11015 defining. We only want to walk into these fields of a type in this
11016 case and not in the general case of a mere reference to the type.
11018 The criterion is as follows: if the field can be an expression, it
11019 must be walked only here. This should be in keeping with the fields
11020 that are directly gimplified in gimplify_type_sizes in order for the
11021 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11022 variable-sized types.
11024 Note that DECLs get walked as part of processing the BIND_EXPR. */
11025 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11027 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11028 if (TREE_CODE (*type_p) == ERROR_MARK)
11029 return NULL_TREE;
11031 /* Call the function for the type. See if it returns anything or
11032 doesn't want us to continue. If we are to continue, walk both
11033 the normal fields and those for the declaration case. */
11034 result = (*func) (type_p, &walk_subtrees, data);
11035 if (result || !walk_subtrees)
11036 return result;
11038 /* But do not walk a pointed-to type since it may itself need to
11039 be walked in the declaration case if it isn't anonymous. */
11040 if (!POINTER_TYPE_P (*type_p))
11042 result = walk_type_fields (*type_p, func, data, pset, lh);
11043 if (result)
11044 return result;
11047 /* If this is a record type, also walk the fields. */
11048 if (RECORD_OR_UNION_TYPE_P (*type_p))
11050 tree field;
11052 for (field = TYPE_FIELDS (*type_p); field;
11053 field = DECL_CHAIN (field))
11055 /* We'd like to look at the type of the field, but we can
11056 easily get infinite recursion. So assume it's pointed
11057 to elsewhere in the tree. Also, ignore things that
11058 aren't fields. */
11059 if (TREE_CODE (field) != FIELD_DECL)
11060 continue;
11062 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11063 WALK_SUBTREE (DECL_SIZE (field));
11064 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11065 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11066 WALK_SUBTREE (DECL_QUALIFIER (field));
11070 /* Same for scalar types. */
11071 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11072 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11073 || TREE_CODE (*type_p) == INTEGER_TYPE
11074 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11075 || TREE_CODE (*type_p) == REAL_TYPE)
11077 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11078 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11081 WALK_SUBTREE (TYPE_SIZE (*type_p));
11082 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11084 /* FALLTHRU */
11086 default:
11087 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11089 int i, len;
11091 /* Walk over all the sub-trees of this operand. */
11092 len = TREE_OPERAND_LENGTH (*tp);
11094 /* Go through the subtrees. We need to do this in forward order so
11095 that the scope of a FOR_EXPR is handled properly. */
11096 if (len)
11098 for (i = 0; i < len - 1; ++i)
11099 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11100 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11103 /* If this is a type, walk the needed fields in the type. */
11104 else if (TYPE_P (*tp))
11105 return walk_type_fields (*tp, func, data, pset, lh);
11106 break;
11109 /* We didn't find what we were looking for. */
11110 return NULL_TREE;
11112 #undef WALK_SUBTREE_TAIL
11114 #undef WALK_SUBTREE
11116 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11118 tree
11119 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11120 walk_tree_lh lh)
11122 tree result;
11123 struct pointer_set_t *pset;
11125 pset = pointer_set_create ();
11126 result = walk_tree_1 (tp, func, data, pset, lh);
11127 pointer_set_destroy (pset);
11128 return result;
11132 tree
11133 tree_block (tree t)
11135 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11137 if (IS_EXPR_CODE_CLASS (c))
11138 return LOCATION_BLOCK (t->exp.locus);
11139 gcc_unreachable ();
11140 return NULL;
11143 void
11144 tree_set_block (tree t, tree b)
11146 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11148 if (IS_EXPR_CODE_CLASS (c))
11150 if (b)
11151 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11152 else
11153 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11155 else
11156 gcc_unreachable ();
11159 /* Create a nameless artificial label and put it in the current
11160 function context. The label has a location of LOC. Returns the
11161 newly created label. */
11163 tree
11164 create_artificial_label (location_t loc)
11166 tree lab = build_decl (loc,
11167 LABEL_DECL, NULL_TREE, void_type_node);
11169 DECL_ARTIFICIAL (lab) = 1;
11170 DECL_IGNORED_P (lab) = 1;
11171 DECL_CONTEXT (lab) = current_function_decl;
11172 return lab;
11175 /* Given a tree, try to return a useful variable name that we can use
11176 to prefix a temporary that is being assigned the value of the tree.
11177 I.E. given <temp> = &A, return A. */
11179 const char *
11180 get_name (tree t)
11182 tree stripped_decl;
11184 stripped_decl = t;
11185 STRIP_NOPS (stripped_decl);
11186 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11187 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11188 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11190 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11191 if (!name)
11192 return NULL;
11193 return IDENTIFIER_POINTER (name);
11195 else
11197 switch (TREE_CODE (stripped_decl))
11199 case ADDR_EXPR:
11200 return get_name (TREE_OPERAND (stripped_decl, 0));
11201 default:
11202 return NULL;
11207 /* Return true if TYPE has a variable argument list. */
11209 bool
11210 stdarg_p (const_tree fntype)
11212 function_args_iterator args_iter;
11213 tree n = NULL_TREE, t;
11215 if (!fntype)
11216 return false;
11218 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11220 n = t;
11223 return n != NULL_TREE && n != void_type_node;
11226 /* Return true if TYPE has a prototype. */
11228 bool
11229 prototype_p (tree fntype)
11231 tree t;
11233 gcc_assert (fntype != NULL_TREE);
11235 t = TYPE_ARG_TYPES (fntype);
11236 return (t != NULL_TREE);
11239 /* If BLOCK is inlined from an __attribute__((__artificial__))
11240 routine, return pointer to location from where it has been
11241 called. */
11242 location_t *
11243 block_nonartificial_location (tree block)
11245 location_t *ret = NULL;
11247 while (block && TREE_CODE (block) == BLOCK
11248 && BLOCK_ABSTRACT_ORIGIN (block))
11250 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11252 while (TREE_CODE (ao) == BLOCK
11253 && BLOCK_ABSTRACT_ORIGIN (ao)
11254 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11255 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11257 if (TREE_CODE (ao) == FUNCTION_DECL)
11259 /* If AO is an artificial inline, point RET to the
11260 call site locus at which it has been inlined and continue
11261 the loop, in case AO's caller is also an artificial
11262 inline. */
11263 if (DECL_DECLARED_INLINE_P (ao)
11264 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11265 ret = &BLOCK_SOURCE_LOCATION (block);
11266 else
11267 break;
11269 else if (TREE_CODE (ao) != BLOCK)
11270 break;
11272 block = BLOCK_SUPERCONTEXT (block);
11274 return ret;
11278 /* If EXP is inlined from an __attribute__((__artificial__))
11279 function, return the location of the original call expression. */
11281 location_t
11282 tree_nonartificial_location (tree exp)
11284 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11286 if (loc)
11287 return *loc;
11288 else
11289 return EXPR_LOCATION (exp);
11293 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11294 nodes. */
11296 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11298 static hashval_t
11299 cl_option_hash_hash (const void *x)
11301 const_tree const t = (const_tree) x;
11302 const char *p;
11303 size_t i;
11304 size_t len = 0;
11305 hashval_t hash = 0;
11307 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11309 p = (const char *)TREE_OPTIMIZATION (t);
11310 len = sizeof (struct cl_optimization);
11313 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11315 p = (const char *)TREE_TARGET_OPTION (t);
11316 len = sizeof (struct cl_target_option);
11319 else
11320 gcc_unreachable ();
11322 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11323 something else. */
11324 for (i = 0; i < len; i++)
11325 if (p[i])
11326 hash = (hash << 4) ^ ((i << 2) | p[i]);
11328 return hash;
11331 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11332 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11333 same. */
11335 static int
11336 cl_option_hash_eq (const void *x, const void *y)
11338 const_tree const xt = (const_tree) x;
11339 const_tree const yt = (const_tree) y;
11340 const char *xp;
11341 const char *yp;
11342 size_t len;
11344 if (TREE_CODE (xt) != TREE_CODE (yt))
11345 return 0;
11347 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11349 xp = (const char *)TREE_OPTIMIZATION (xt);
11350 yp = (const char *)TREE_OPTIMIZATION (yt);
11351 len = sizeof (struct cl_optimization);
11354 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11356 xp = (const char *)TREE_TARGET_OPTION (xt);
11357 yp = (const char *)TREE_TARGET_OPTION (yt);
11358 len = sizeof (struct cl_target_option);
11361 else
11362 gcc_unreachable ();
11364 return (memcmp (xp, yp, len) == 0);
11367 /* Build an OPTIMIZATION_NODE based on the current options. */
11369 tree
11370 build_optimization_node (void)
11372 tree t;
11373 void **slot;
11375 /* Use the cache of optimization nodes. */
11377 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11378 &global_options);
11380 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11381 t = (tree) *slot;
11382 if (!t)
11384 /* Insert this one into the hash table. */
11385 t = cl_optimization_node;
11386 *slot = t;
11388 /* Make a new node for next time round. */
11389 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11392 return t;
11395 /* Build a TARGET_OPTION_NODE based on the current options. */
11397 tree
11398 build_target_option_node (void)
11400 tree t;
11401 void **slot;
11403 /* Use the cache of optimization nodes. */
11405 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11406 &global_options);
11408 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11409 t = (tree) *slot;
11410 if (!t)
11412 /* Insert this one into the hash table. */
11413 t = cl_target_option_node;
11414 *slot = t;
11416 /* Make a new node for next time round. */
11417 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11420 return t;
11423 /* Determine the "ultimate origin" of a block. The block may be an inlined
11424 instance of an inlined instance of a block which is local to an inline
11425 function, so we have to trace all of the way back through the origin chain
11426 to find out what sort of node actually served as the original seed for the
11427 given block. */
11429 tree
11430 block_ultimate_origin (const_tree block)
11432 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11434 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11435 nodes in the function to point to themselves; ignore that if
11436 we're trying to output the abstract instance of this function. */
11437 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11438 return NULL_TREE;
11440 if (immediate_origin == NULL_TREE)
11441 return NULL_TREE;
11442 else
11444 tree ret_val;
11445 tree lookahead = immediate_origin;
11449 ret_val = lookahead;
11450 lookahead = (TREE_CODE (ret_val) == BLOCK
11451 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11453 while (lookahead != NULL && lookahead != ret_val);
11455 /* The block's abstract origin chain may not be the *ultimate* origin of
11456 the block. It could lead to a DECL that has an abstract origin set.
11457 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11458 will give us if it has one). Note that DECL's abstract origins are
11459 supposed to be the most distant ancestor (or so decl_ultimate_origin
11460 claims), so we don't need to loop following the DECL origins. */
11461 if (DECL_P (ret_val))
11462 return DECL_ORIGIN (ret_val);
11464 return ret_val;
11468 /* Return true if T1 and T2 are equivalent lists. */
11470 bool
11471 list_equal_p (const_tree t1, const_tree t2)
11473 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11474 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11475 return false;
11476 return !t1 && !t2;
11479 /* Return true iff conversion in EXP generates no instruction. Mark
11480 it inline so that we fully inline into the stripping functions even
11481 though we have two uses of this function. */
11483 static inline bool
11484 tree_nop_conversion (const_tree exp)
11486 tree outer_type, inner_type;
11487 int outer_is_pts_p, inner_is_pts_p;
11489 if (!CONVERT_EXPR_P (exp)
11490 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11491 return false;
11492 if (TREE_OPERAND (exp, 0) == error_mark_node)
11493 return false;
11495 outer_type = TREE_TYPE (exp);
11496 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11498 if (!inner_type)
11499 return false;
11501 outer_is_pts_p = (POINTER_TYPE_P (outer_type)
11502 && upc_shared_type_p (TREE_TYPE (outer_type)));
11503 inner_is_pts_p = (POINTER_TYPE_P (inner_type)
11504 && upc_shared_type_p (TREE_TYPE (inner_type)));
11506 /* UPC pointer-to-shared types have special
11507 equivalence rules that must be checked. */
11508 if (outer_is_pts_p && inner_is_pts_p
11509 && lang_hooks.types_compatible_p)
11510 return lang_hooks.types_compatible_p (outer_type, inner_type);
11512 /* UPC pointer-to-shared types are not interchangeable
11513 with integral types. */
11514 if (outer_is_pts_p || inner_is_pts_p)
11515 return false;
11517 /* Use precision rather then machine mode when we can, which gives
11518 the correct answer even for submode (bit-field) types. */
11519 if ((INTEGRAL_TYPE_P (outer_type)
11520 || POINTER_TYPE_P (outer_type)
11521 || TREE_CODE (outer_type) == OFFSET_TYPE)
11522 && (INTEGRAL_TYPE_P (inner_type)
11523 || POINTER_TYPE_P (inner_type)
11524 || TREE_CODE (inner_type) == OFFSET_TYPE))
11525 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11527 /* Otherwise fall back on comparing machine modes (e.g. for
11528 aggregate types, floats). */
11529 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11532 /* Return true iff conversion in EXP generates no instruction. Don't
11533 consider conversions changing the signedness. */
11535 static bool
11536 tree_sign_nop_conversion (const_tree exp)
11538 tree outer_type, inner_type;
11540 if (!tree_nop_conversion (exp))
11541 return false;
11543 outer_type = TREE_TYPE (exp);
11544 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11546 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11547 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11550 /* Strip conversions from EXP according to tree_nop_conversion and
11551 return the resulting expression. */
11553 tree
11554 tree_strip_nop_conversions (tree exp)
11556 while (tree_nop_conversion (exp))
11557 exp = TREE_OPERAND (exp, 0);
11558 return exp;
11561 /* Strip conversions from EXP according to tree_sign_nop_conversion
11562 and return the resulting expression. */
11564 tree
11565 tree_strip_sign_nop_conversions (tree exp)
11567 while (tree_sign_nop_conversion (exp))
11568 exp = TREE_OPERAND (exp, 0);
11569 return exp;
11572 /* Avoid any floating point extensions from EXP. */
11573 tree
11574 strip_float_extensions (tree exp)
11576 tree sub, expt, subt;
11578 /* For floating point constant look up the narrowest type that can hold
11579 it properly and handle it like (type)(narrowest_type)constant.
11580 This way we can optimize for instance a=a*2.0 where "a" is float
11581 but 2.0 is double constant. */
11582 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11584 REAL_VALUE_TYPE orig;
11585 tree type = NULL;
11587 orig = TREE_REAL_CST (exp);
11588 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11589 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11590 type = float_type_node;
11591 else if (TYPE_PRECISION (TREE_TYPE (exp))
11592 > TYPE_PRECISION (double_type_node)
11593 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11594 type = double_type_node;
11595 if (type)
11596 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11599 if (!CONVERT_EXPR_P (exp))
11600 return exp;
11602 sub = TREE_OPERAND (exp, 0);
11603 subt = TREE_TYPE (sub);
11604 expt = TREE_TYPE (exp);
11606 if (!FLOAT_TYPE_P (subt))
11607 return exp;
11609 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11610 return exp;
11612 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11613 return exp;
11615 return strip_float_extensions (sub);
11618 /* Strip out all handled components that produce invariant
11619 offsets. */
11621 const_tree
11622 strip_invariant_refs (const_tree op)
11624 while (handled_component_p (op))
11626 switch (TREE_CODE (op))
11628 case ARRAY_REF:
11629 case ARRAY_RANGE_REF:
11630 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11631 || TREE_OPERAND (op, 2) != NULL_TREE
11632 || TREE_OPERAND (op, 3) != NULL_TREE)
11633 return NULL;
11634 break;
11636 case COMPONENT_REF:
11637 if (TREE_OPERAND (op, 2) != NULL_TREE)
11638 return NULL;
11639 break;
11641 default:;
11643 op = TREE_OPERAND (op, 0);
11646 return op;
11649 static GTY(()) tree gcc_eh_personality_decl;
11651 /* Return the GCC personality function decl. */
11653 tree
11654 lhd_gcc_personality (void)
11656 if (!gcc_eh_personality_decl)
11657 gcc_eh_personality_decl = build_personality_function ("gcc");
11658 return gcc_eh_personality_decl;
11661 /* Try to find a base info of BINFO that would have its field decl at offset
11662 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11663 found, return, otherwise return NULL_TREE. */
11665 tree
11666 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11668 tree type = BINFO_TYPE (binfo);
11670 while (true)
11672 HOST_WIDE_INT pos, size;
11673 tree fld;
11674 int i;
11676 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11677 return binfo;
11678 if (offset < 0)
11679 return NULL_TREE;
11681 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11683 if (TREE_CODE (fld) != FIELD_DECL)
11684 continue;
11686 pos = int_bit_position (fld);
11687 size = tree_low_cst (DECL_SIZE (fld), 1);
11688 if (pos <= offset && (pos + size) > offset)
11689 break;
11691 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11692 return NULL_TREE;
11694 if (!DECL_ARTIFICIAL (fld))
11696 binfo = TYPE_BINFO (TREE_TYPE (fld));
11697 if (!binfo)
11698 return NULL_TREE;
11700 /* Offset 0 indicates the primary base, whose vtable contents are
11701 represented in the binfo for the derived class. */
11702 else if (offset != 0)
11704 tree base_binfo, found_binfo = NULL_TREE;
11705 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11706 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11708 found_binfo = base_binfo;
11709 break;
11711 if (!found_binfo)
11712 return NULL_TREE;
11713 binfo = found_binfo;
11716 type = TREE_TYPE (fld);
11717 offset -= pos;
11721 /* Returns true if X is a typedef decl. */
11723 bool
11724 is_typedef_decl (tree x)
11726 return (x && TREE_CODE (x) == TYPE_DECL
11727 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11730 /* Returns true iff TYPE is a type variant created for a typedef. */
11732 bool
11733 typedef_variant_p (tree type)
11735 return is_typedef_decl (TYPE_NAME (type));
11738 /* Warn about a use of an identifier which was marked deprecated. */
11739 void
11740 warn_deprecated_use (tree node, tree attr)
11742 const char *msg;
11744 if (node == 0 || !warn_deprecated_decl)
11745 return;
11747 if (!attr)
11749 if (DECL_P (node))
11750 attr = DECL_ATTRIBUTES (node);
11751 else if (TYPE_P (node))
11753 tree decl = TYPE_STUB_DECL (node);
11754 if (decl)
11755 attr = lookup_attribute ("deprecated",
11756 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11760 if (attr)
11761 attr = lookup_attribute ("deprecated", attr);
11763 if (attr)
11764 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11765 else
11766 msg = NULL;
11768 if (DECL_P (node))
11770 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11771 if (msg)
11772 warning (OPT_Wdeprecated_declarations,
11773 "%qD is deprecated (declared at %s:%d): %s",
11774 node, xloc.file, xloc.line, msg);
11775 else
11776 warning (OPT_Wdeprecated_declarations,
11777 "%qD is deprecated (declared at %s:%d)",
11778 node, xloc.file, xloc.line);
11780 else if (TYPE_P (node))
11782 tree what = NULL_TREE;
11783 tree decl = TYPE_STUB_DECL (node);
11785 if (TYPE_NAME (node))
11787 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11788 what = TYPE_NAME (node);
11789 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11790 && DECL_NAME (TYPE_NAME (node)))
11791 what = DECL_NAME (TYPE_NAME (node));
11794 if (decl)
11796 expanded_location xloc
11797 = expand_location (DECL_SOURCE_LOCATION (decl));
11798 if (what)
11800 if (msg)
11801 warning (OPT_Wdeprecated_declarations,
11802 "%qE is deprecated (declared at %s:%d): %s",
11803 what, xloc.file, xloc.line, msg);
11804 else
11805 warning (OPT_Wdeprecated_declarations,
11806 "%qE is deprecated (declared at %s:%d)", what,
11807 xloc.file, xloc.line);
11809 else
11811 if (msg)
11812 warning (OPT_Wdeprecated_declarations,
11813 "type is deprecated (declared at %s:%d): %s",
11814 xloc.file, xloc.line, msg);
11815 else
11816 warning (OPT_Wdeprecated_declarations,
11817 "type is deprecated (declared at %s:%d)",
11818 xloc.file, xloc.line);
11821 else
11823 if (what)
11825 if (msg)
11826 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11827 what, msg);
11828 else
11829 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11831 else
11833 if (msg)
11834 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11835 msg);
11836 else
11837 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11843 #include "gt-tree.h"